Hibernate changes database change - oracle

I am using Struts and Hibernate 2.0 in my project. My database has changed from oracle 10g to Oracle 11g in my project.
Can you please tell me what changes do I need to make in my Hibernate configuration?
Also Are there any changes required in my Java code.
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:#<host>:<port>:<sid></property>
<property name="connection.username">username</property>
<property name="connection.password">password</property>
<property name="cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.show_sql">false</property>
</session-factory>
</hibernate-configuration>

Just update driver (Jar file) to Oracle Database 11g Release 2 JDBC Drivers

Your XML doesn't seem like it was configured to Oracle 10g , it shows MySQL..
Anyways do this to solve your question.
Just change the Driver name and URL i.e connection string and also change the Dialect
search for Oracle Dialect online and don't forget to add Jars!!! :)

If your data access logic is implemented in Hibernate then your code is portable. Meaning you really don’t need to change anything specific to database change in your case apart from Driver if its causing any trouble . For instance, if you are changing database from MYSQL to Oracle then probably need to change dialect part, Connection properties of your hibernate configuration file alone plus your database specific driver.
Hope this is helpful!

Yes you need to change the dialect name. Sorry I am posting another answer as I am not able to add another comment.
Find for Oracle11g dialect name online, Dialects are classes that convert HQL(hibernate query language to actually Database query), it may say ClassNotFoundException, if this happens then just search for the Hibernate jar that has Oracle11g Dialect Class in it. As I am pretty sure that this jar is NOT available with the JDBCDriver jars that ORacle provides to connect to database.
The Dialect Jars are a part of Hibernate

Related

Programatically set v$session program property

I found some answers on the problem but none that i could make work in my case. My problem is that I load a datasource from my JBoss configuration with spring:
<xa-datasource jndi-name="java:jboss/jdbc/oracleDatasource" pool-name="jdbc/oracleDatasource" enabled="true">
<xa-datasource-property name="URL">
jdbc:oracle:thin:#URL:1522:SID
</xa-datasource-property>
<xa-datasource-property name="connectionProperties">
v$session.program=MyAPP
</xa-datasource-property>
<driver>oracle-jdbc</driver>
The spring loading is made as follows:
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:jboss/jdbc/oracleDatasource"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="dataSource"/>
</bean>
As you can see, I have set the v$session.program property in JBoss, it works well.
The problem is that i have several applications (war) that can be deployed on the same JBoss server, using this configuration. What I want to do in this case is to have each of my application to have its own name written in the v$session.program property.
So basically, i would like to be able to load the same datasource on each app but to have each of them using its own name to log the program property in oracle DB. Is it possible or do I have to have one datasource for each application hosted?
The only thing you need it to intercept each call of getConnection from connection pool.
You must obtain a real Oracle connection - not a proxy - and call the setClientInfo on 12c or setEndToEndMetrics in older versions to set the action / client / module identification.
An example see here.
Also note that the use of dbms_application_info for this same purpose works as well, but this produces a one server roundtrip too much. The setClientInfo doesn't produce server call, but stores this information for the next statement execution (which is the preformance saving approach).
Also note that to use this feature, your driver must match perfectly with your database - the strange exeptions you can see while setting teh client info are in most cases caused by the incompatibility of teh JDBC driver and the RDBMS.
If putting this information into v$session.module or v$session.client_info is an option, you can do using Java code.
All you need to do is call dbms_application_info.set_module() or dbms_application_info.set_client_info() after your Java code obtained the connection from the datasource.
Something like this:
Connection conn = ... // get connection from the DataSource
CallableStatement cstmt = conn.prepareCall("{call dbms_application_info.set_client_info(?)}");
cstmt.setString(1, "Some interesting information");
cstmt.execute();

Spring session with JDBC integration

I am new in Spring Session and want to use embedded database to store session information. I follow all the steps in
http://docs.spring.io/spring-session/docs/current/reference/html5/guides/httpsession-jdbc.html using spring-session-jdbc of version 1.2.0.but using Spring-web of 3.2.4 only
The following error is shown repeatly:
Caused by: org.postgresql.util.PSQLException: ERROR: relation "spring_session" does not exist 'Position: 13
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2198)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1927)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:561)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:419)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:365)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:105)
at org.springframework.jdbc.core.JdbcTemplate$2.doInPreparedStatement(JdbcTemplate.java:824)
at org.springframework.jdbc.core.JdbcTemplate$2.doInPreparedStatement(JdbcTemplate.java:818)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:589)
... 21 more'
This issue already haunted me for days. Please help.
Here is the xml configuration
<bean class="org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessi‌​onConfiguration"/>
<jdbc:embedded-database id="dataSource02" type="H2">
<jdbc:script location="classpath:org/springframework/session/jdbc/schema-h2.sql"/>
</jdbc:embedded-database>
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<constructor-arg ref="dataSource02"/>
</bean>
Your problem stems from the fact that you use multiple data sources, since the provided stacktrace includes PostgreSQL JDBC driver classes while you are configuring a secondary, embedded data source to storing sessions.
Spring Session configuration picks up your primary data source (PostgreSQL) and expects to find session tables there.
I'd suggest you to use your primary data source to store session data, but if you insist on having secondary/embedded data source for that purpose you need to override JdbcOperationsSessionRepository bean provided by JdbcHttpSessionConfiguration#sessionRepository with your own instance which is created using your secondary data source and its appropriate tra saction manager. Note that bean must be named sessionRepository in order to override one from JdbcHttpSessionConfiguration.

Getting DDL Before Updating the Schema in Hibernate Spring Applications

In a Spring-Hibernate application, hibernate.show_sql=true in log4j property file is enough to show to hibernate generated query. But what we have to do if we need the actual sql query.(For production environment i need to observer and verify the query before updating the schema).
What i am going to do is, after the first schema generation in production( by hibernate itself) i dont want hibernate to update the schema(DDL).I want to hijack the alter, update (DDL) queries and after verification i would like to run those scripts manually in DB.
Is there any approach to do that?
Hibernate has build-in a function to enable the logging of all the generated SQL statements to the console. You can enable it by add a show_sql property in the Hibernate configuration file hibernate.cfg.xml. This function is good for basic troubleshooting, and to see what’s Hibernate is doing behind.
hibernate.cfg.xml
show_sql
Enable the logging of all the generated SQL statements to the console
<property name="show_sql">true</property>
format_sql
Format the generated SQL statement to make it more readable, but takes up more screen space.
<property name="format_sql">true</property>
use_sql_comments
Hibernate will put comments inside all generated SQL statements to hint what’s the generated SQL trying to do
<property name="use_sql_comments">true</property>
log4j.properties
log4j.logger.org.hibernate=INFO, hb
log4j.logger.org.hibernate.SQL=DEBUG ## is equivalent to hibernate.show_sql=true
log4j.logger.org.hibernate.type=TRACE ## allows you to see the binding parameters
log4j.logger.org.hibernate.hql.ast.AST=info
log4j.logger.org.hibernate.tool.hbm2ddl=warn
log4j.logger.org.hibernate.hql=debug
log4j.logger.org.hibernate.cache=info
log4j.logger.org.hibernate.jdbc=debug
log4j.appender.hb=org.apache.log4j.ConsoleAppender
log4j.appender.hb.layout=org.apache.log4j.PatternLayout
log4j.appender.hb.layout.ConversionPattern=HibernateLog --> %d{HH:mm:ss} %-5p %c - %m%n
log4j.appender.hb.Threshold=TRACE
log4j.logger.org.hibernate.tool.hbm2ddl=debug
To print the bind parameters as well, add the following to your log4j.properties file:
log4j.logger.net.sf.hibernate.type=debug
Add the following configurations in log4j.properties file
logs the SQL statements
log4j.logger.org.hibernate.SQL=debug
Logs the JDBC parameters passed to a query
log4j.logger.org.hibernate.type=trace
Enable the show sql property in hibernate configuration file as follows.
<property name="show_sql">true</property>
For more details, check the post

Using nhibernate and oracle using DbProviderFactories

In our system we are using nhibernate and Oracle. We have always a lot of deployment problems because the relationship between Oracle.DataAccess.dll and the Oracle client that is installed in our client's server is a mess.
Sometimes we can change the installed oracle client, sometimes not. Sometimes we need the 32bit version of the dll, sometimes the 64bit one. I wish I could change oracle for a friendlier database, but that's not an option for now.
One way to get rid of all this mess is to use DbProviderFactories, so I could have no reference for the Oracle.DataAccess.dll in my project and use whichever version is installed on the server.
How can I configure nhibernate to use a DbProviderFactory?
My configuration:
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.Oracle9iDialect</property>
<property name="connection.driver_class">NHibernate.Driver.OracleDataClientDriver</property>
<property name="connection.connection_string">Data Source=XE; User Id=*****; Password=******;</property>
<property name="current_session_context_class">NHibernate.Context.ManagedWebSessionContext, NHibernate</property>
<property name="sql_exception_converter">Way2Pim.Data.NHibernate.SqlExceptions.SqlExceptionConverter, Way2Pim.Data</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.keywords">none</property>
<mapping assembly="MyAssembly" />
</session-factory>
</hibernate-configuration>
NHibernate already uses the dbproviderfactory if loading per reflection fails. see NHibernate.Driver.ReflectionBasedDriver
this feature is available since
Revision: bb904343e18b693f6d1e13b357c305fdd46ff5ee
Author: Fabio Maulo
Date: 14.08.2010 19:37:12
Message:
Refactoring of ReflectionBasedDriver in order to use DbProviderFactories
Got it!
Nhibernate has a bug concerning Oracle and DBProviderFactories. If you look at OracleDataClientDriver class, it derives from ReflectionBasedDriver.
In it's constructor, ReflectionBasedDriver loads the Oracle.DataAccess provider from DbProviderFactory correctly. After that, the OracleDataClientDriver constructor tries to get the OracleCommand type from the loaded assemblies, which of course will result in null (the Oracle.DataAccess is not in memory yet).
A quick fix is to create your own Driver copying everything from OracleDataClientDriver, but replacing all lines of its constructor with these:
System.Type oracleCommandType = base.CreateCommand().GetType();
oracleCommandBindByName = oracleCommandType.GetProperty("BindByName");
System.Type parameterType = oracleCommandType.Assembly.GetType("Oracle.DataAccess.Client.OracleParameter");
oracleDbType = parameterType.GetProperty("OracleDbType");
System.Type oracleDbTypeEnum = oracleCommandType.Assembly.GetType("Oracle.DataAccess.Client.OracleDbType");
oracleDbTypeRefCursor = System.Enum.Parse(oracleDbTypeEnum, "RefCursor");
And that's it, no Oracle.DataAccess.dll in your project anymore :)

Spring JdbcTemplate ConnectionPooling Configuration

I am working on a Spring MVC application in which I have recently been convinced to revamp my database code. Before I was using very traditional JDBC code that I have been told was very "old school" because of the boilerplate code. I have been making the transition to using JdbcTemplate with Spring.
I have configured a bean like shown below in my applicationContext.xml file.
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:ip-address:port/dbName"/>
<property name="username" value="myUsername"/>
<property name="password" value="mypassword"/>
</bean>
I have run tests just to make sure everything is working and it is.
My question is, I am aware that I am using the Commons DBCP package which uses the
following packages
commons-dbcp package
commons-pool package
Again, I am very inexperienced with this, so I apologize if I am mis referencing something or am explaining something incorrectly.
I have followed what most of the tutorials have said to do and specified a jdbcTemplate and injected the dataSource bean into it, but this doesnt really refer to my question.
What I would really like to know is, am I using ConnectionPooling with this configuration?
If so, is it being done behind the scenes, or do I need to specify to do it somewhere?
I have looked at the documentation at Here which gives the following, but I am not sure exactly how to interpret it.
"here are several Database Connection Pools already available, both within Apache products and elsewhere. This Commons package provides an opportunity to coordinate the efforts required to create and maintain an efficient, feature-rich package under the ASF license.
The commons-dbcp package relies on code in the commons-pool package to provide the underlying object pool mechanisms that it utilizes."
I also looked at the Configuration Page
and based on this page, I would think that I am able to do ConnectionPooling, but may need to specify additional parameters in my dataSource bean.
Can somebody please answer my questions or point me in the right direction?
Yes you are using connection pooling.
here is another thread you might find interesting
http://forum.springsource.org/showthread.php?t=40598
Also most of the links you specified above will provide additional information on parameters that can be set.

Resources