Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to hibernate Session while running the test cases

 
smitha rai
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a datasource (jdbc/hibernatespring) configured on the websphere server. I am able to get connection while executing the JSPs (which intenally calls the DAOs) and display the contents appropriately.

I have written JUnit test cases for the DAOs and while executing the test cases, it could not be able to find the datasource and throws "NoInitialContextException" exception. In this scenario, definitely the test cases are executed in a different JVM. How to get the hibernate session in such scenario?


hibernate.cfg.xml
========================================================================
<hibernate-configuration>

<session-factory name="java:hibernate/SessionFactory">
<property name="connection.datasource">jdbc/hibernatespring</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="show_sql">false</property>

<!-- Add the resource config files here-->
<mapping resource="user.hbm.config.xml"/>
<mapping resource="insurance.hbm.config.xml"/>
</session-factory>

</hibernate-configuration>

==========================================================================
I am using the following code to get the hibernate session
public Session getHibernateSession() {

SessionFactory sessionFactory =
new Configuration()
.configure("hibernate.cfg.xml")
.buildSessionFactory();
Session session = sessionFactory.openSession();

return session;
}

============================================================================
The exception is:

[FATAL] DatasourceConnectionProvider - Could not find datasource: jdbc/hibernatespring <javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial>javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:652)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:255)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:292)
at javax.naming.InitialContext.lookup(InitialContext.java:359)
at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:44)
at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:80)
at org.hibernate.cfg.SettingsFactory.createConnectionProvider(SettingsFactory.java:362)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:60)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:1463)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1004)
at demo.client.HibernateClient.getHibernateSession(HibernateClient.java:46)
at demo.client.select.user.UserSelectClient.readData(UserSelectClient.java:35)
at demo.client.select.user.UserSelectClient.main(UserSelectClient.java:56)

org.hibernate.HibernateException: Could not find datasource
at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:48)
at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:80)
at org.hibernate.cfg.SettingsFactory.createConnectionProvider(SettingsFactory.java:362)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:60)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:1463)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1004)
at demo.client.HibernateClient.getHibernateSession(HibernateClient.java:46)
at demo.client.select.user.UserSelectClient.readData(UserSelectClient.java:35)
at demo.client.select.user.UserSelectClient.main(UserSelectClient.java:56)
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:652)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:255)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:292)
at javax.naming.InitialContext.lookup(InitialContext.java:359)
 
Emanuel Kadziela
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Setup the datasource manually in your test cases and assign it to that naming context (jdbc/hibernatespring which you later reference in hibernate.cfg.xml). The last time I ran JUnit tests on this stuff, I used the setUp method of my main test class.
 
Ryan Day
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This thread was helpful (even though it is old). I ran into a similar problem using Rational Software Architect. My datasources were configured through JNDI references on my local WebSphere server.

The solution was to configure a local JDBC connection (I did mine through Database explorer).

Here was the section I used from hibernate.cfg.xml (I commented out the section that used the JNDI reference during my testing):

<property name="hibernate.connection.url">jdbc:microsoft:sqlserver://localhost:1433;databaseName=test_db</property>
<property name="hibernate.connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
<property name="hibernate.connection.username">(username)</property>
<property name="hibernate.connection.password">(password)</property>
 
will edw
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had this problem.

It turned out to be the fact I had named the session factory, without declaring a jndi property. If a session is named, then you must set a property called hibernate.jndi.class and hibernate.jndi.url.

either do this, or remove your session factory name entirely, from

<session-factory name="java:hibernate/SessionFactory">

to

<session-factory>
 
Unknown UnknownToYou
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem is probably due to a mis-configured hiubernate.cfg.xml

Error...

SessionFactory factory config.buildSessionFactory ;

javax.naming.NoInitialContextException:



CAREFULLY REVIEW IT FOR ERRORS, TYPOS, and entries not completed.

example:

<session-factory name=""> //Can cause an error

<property name="transaction_factory_class">org.hibernate.transaction.JDBCTransactionFactory</property> //WILL Cause an error
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unknown UnknownToYou, please check your private messages.
 
Gupta Ashish
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its a useful topic, it helped me in resolving the exception 'javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial'.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic