I have an application which is
not running in a container (no JBoss, etc). Someday it might migrate there after a lot of changes, but at this point we're making small changes to get Hibernate support into the code and eliminate a lot of custom SQL and entity management.
One of the goals was to support changing that database connection very easily so, for example, some tests could be run against a different database. To do this, we use Spring in a very minimal way. Our hibernate.cfg.xml file looks something like this:
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.provider_class">org.example.CustomConnectionProvider</property>
<property name="connection.bean_name">exampleDataSource</property>
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">false</property>
<property name="hbm2ddl.auto">verify</property>
<mapping resource="org/example/Pojo.hbm.xml" />
</session-factory>
</hibernate-configuration>
Spring is used to inject the actual DataSource. To make it uglier :-) we use some code that loads applicationContext.xml and does property subsitution a la Ant's syntax so that passwords and such are not hardcoded in that file (which is in the jar) but can be substituted from an external property file. There are business reasons that require us to do that, but in any event, the applicationContext.xml looks like this:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="exampleDataSource" class="org.example.exampleDataSource">
<property name="URL"
value="jdbc:oracle:thin:@//${example.db.server}:${example.db.port}/${example.db.name}.example.org" />
<property name="user" value="${example.db.user}" />
</bean>
</beans>
All this works just fine. Um, so why am I writing all this?
I made an "innocuous" little change to hibernate.cfg.xml in preparation for expanding our setup to handle connecting to two databases. I changed
Code:
<session-factory>
to
Code:
<session-factory name="exampleSessionFactory">
I now get an exception, which is not fatal,
Code:
javax.naming.NoInitialContextException: Cannot instantiate class: org.jnp.interfaces.NamingContextFactory [Root exception is java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory]
I've come to the conclusion, based on Googling, that I can't put the name= parameter unless I also set up JNDI. But (1) I can't actually find that documented; my google-fu has failed me there, and (2) I'm wondering if my conclusion is wrong and there is a way to name the session-factory without using JNDI. At some level it doesn't really matter as the name is, for now anyway, purely documentary.