Configuring simple-jndi with Hibernate

114 views
Skip to first unread message

axuriza

unread,
Jun 11, 2008, 5:39:46 PM6/11/08
to osjava, aur...@accovia.com
Hi,

I'm modifying an application somebody else did like 2 years ago, based
on Hibernate and simple-jndi. However I'm having trouble understanding
what he did and -even worst- why it is not working for me. I hope you
could help me.

In the conf/ directory of the project, several .properties files are
defined: hibernate.properties, jndi.properties and log4j.properties.
Besides, a heatinfo.hibernate.cfg.xml file exist in the same
directory.

An abstract of the hibernate.properties file:
hibernate.connection.url=jdbc:jtds:sqlserver://
heatdb.windows.accovia.com:1433;DatabaseName=Heat;SelectMethod=cursor
hibernate.connection.driver_class=net.sourceforge.jtds.jdbc.Driver
hibernate.connection.username=heatread
hibernate.connection.password=******
hibernate.jdbc.use_scrollable_resultset=false
hibernate.connection.pool_size=5
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statements=50
hibernate.dialect=org.hibernate.dialect.SQLServerDialect
hibernate.show_sql=false
hibernate.use_outer_join=true
hibernate.transaction.factory_class=org.hibernate.transaction.JDBCTransactionFactory
hibernate.current_session_context_class=thread
hibernate.transaction.auto_close_session=true
hibernate.jndi.class=org.osjava.sj.memory.MemoryContextFactory

The jdni.properties looks like this:
java.naming.factory.initial=org.osjava.sj.memory.MemoryContextFactory
org.osjava.sj.root=conf/
org.osjava.sj.jndi.shared=true
org.osjava.sj.colon.replace=--

And the heatinfo.hibernate.cfg.xml file is shown below:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-
configuration-3.0.dtd">

<hibernate-configuration>
<!-- a SessionFactory instance listed as /jndi/name -->
<session-factory
name="java:hibernate/SessionFactory">
<!-- mapping files -->
<mapping resource="com/accovia/heat/CallLog.hbm.xml"/>
</session-factory>
</hibernate-configuration>

I included the location of the simple-jndi.jar file in the env
variable CLASSPATH:
CLASSPATH=.;C:\Documents and Settings\auriza\workspace\heatinfo\conf;C:
\ProgramFiles\Java\jre1.6.0_05\lib\ext\QTJava.zip;C:
\SvnStat-0.9\SvnStat-all.jar;C:\Docu
ments and Settings\auriza\workspace\heatinfo\target\classes\simple-
jndi-0.11.3.jar

I'm building the application using maven2, calling the mvn package
goal. It includes the unit tests and here is where it fails. Same if I
try to build it using eclipse and the maven2 plugin, it fails. I
debugged the application and the problem occurs when a new instance of
the DAO is requested.

The internal attribute sessionFactory is generated initialized as
shown in the code below:

private final SessionFactory sessionFactory = getSessionFactory();

protected SessionFactory getSessionFactory() {
try {
return (SessionFactory) new
InitialContext().lookup("SessionFactory");
}
catch (Exception e) {
log.error("Could not locate SessionFactory in JNDI", e);
throw new IllegalStateException("Could not locate
SessionFactory in JNDI");
}
}

but the lookup method of the instance of the InitialContext returns
null. generating a NullPointerException when the DAO tries to get the
information from the databas.

I suppose I'm misconfiguring something but I don't know what it could
be. Any ideas would be very welcome.

Thanks,

Alejandro

Henri Yandell

unread,
Jun 21, 2008, 3:00:25 AM6/21/08
to osj...@googlegroups.com, aur...@accovia.com
Sorry for the slowness in replying - was it originally a Maven2
project, or are you Maven2-izing it? I've noticed that the semantics
between Maven1 and Maven2 changed for local files and that confuses
things.

Looking at your description below - simple-jndi should be a dependency
in your pom.xml with test scope. Simple-JNDI itself has a Maven 2
build now and that should show you how to set up the <testResources>
block to pick up the configuration, though I'll include that block
here:

<testResources>
<testResource>
<directory>${basedir}/src/test/config</directory>
<targetPath>config</targetPath>
</testResource>
<testResource>
<directory>${basedir}/src/test</directory>
<includes>
<include>jndi.properties</include>
</includes>
</testResource>
</testResources>

You'll need something like that for your setup, though the directories
will be different.

Hen

Reply all
Reply to author
Forward
0 new messages