Alvin
Alvin

Reputation: 10468

Hibernate database connection configuration

We have 2 different server environments using the same Hibernate configuration. One server has JNDI support for datasource, but the other does not. Currently the Hibernate configuration is configured to use JNDI, which is causing problem on the server that does not support JNDI.

I have also tried to put the direct JDBC configuration together with JNDI configuration into the configuration file, but it looks like hibernate always favors JNDI over direct JDBC configuration if both exist.

My question is, will it be the same if both JNDI and connection_provider configuration both exists? Will Hibernate still use JNDI over connection_provider? Or is there any way to change the precedence of the database connection property?

I do not have access to the server all the time, so I thought I do ask the question before my window of the sever time.

Thanks in advance.

Upvotes: 1

Views: 6751

Answers (2)

Brian Deterling
Brian Deterling

Reputation: 13734

Use the URL method in the configuration file. In the code, manually try to get a connection to the JNDI source using the context.lookup("java:comp/env/..."). If that succeeds, set the Hibernate data source property and unset the url-related properties. If it fails let Hibnerate continue with the url configuration.

if (jndiDataSourceExists()) {
  configuration.setProperty(Environment.DATASOURCE, "jdbc/jndiname")
  configuration.setProperty(Environment.URL, null);
}
configuration.buildSessionFactory();

Upvotes: 1

Pascal Thivent
Pascal Thivent

Reputation: 570615

We have 2 different server environments using the same Hibernate configuration.

Well, it may not be the expected answer but I have the feeling that you are creating the problem yourself here: if your environments are different, use different configurations. What I would do is the following:

  • put everything common in hibernate.cfg.xml (bundled in the application)
  • put specific things in hibernate.properties files and place the "right one" in a root directory of the class path for each application (but outside the application).

For example, this would be the content of the hibernate.properties for the "JNDI environment":

hibernate.connection.datasource = java:/comp/env/jdbc/test
hibernate.transaction.factory_class = org.hibernate.transaction.JTATransactionFactory
hibernate.transaction.manager_lookup_class = org.hibernate.transaction.JBossTransactionManagerLookup
hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect

And this would be the content of the hibernate.properties for the "non JNDI environment":

hibernate.connection.driver_class = org.postgresql.Driver
hibernate.connection.url = jdbc:postgresql://localhost/mydatabase
hibernate.connection.username = myuser
hibernate.connection.password = secret
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statements=50
hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect

Upvotes: 1

Related Questions