*
The moose likes Object Relational Mapping and the fly likes InitialContext did not implement EventContext Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "InitialContext did not implement EventContext" Watch "InitialContext did not implement EventContext" New topic
Author

InitialContext did not implement EventContext

zuo liao
Greenhorn

Joined: Oct 11, 2007
Posts: 8
I use hibernate3.2, oracle9.2 and websphere 5.1. After I try to configure jndi for the sessionfactory I get the error:

[10/11/07 10:07:33:212 EDT] 75667566 SessionFactor I org.hibernate.impl.SessionFactoryImpl building session factory
[10/11/07 10:07:33:961 EDT] 75667566 SessionFactor I org.hibernate.impl.SessionFactoryObjectFactory Factory name: java:comp/HibernateSessionFactory
[10/11/07 10:07:33:961 EDT] 75667566 NamingHelper I org.hibernate.util.NamingHelper JNDI InitialContext properties:{}
[10/11/07 10:07:33:961 EDT] 75667566 SessionFactor I org.hibernate.impl.SessionFactoryObjectFactory Bound factory to JNDI name: java:comp/HibernateSessionFactory
[10/11/07 10:07:33:961 EDT] 75667566 SessionFactor W org.hibernate.impl.SessionFactoryObjectFactory InitialContext did not implement EventContext[10/11/07 10:08:31:814 EDT] 75667566 SystemOut O !!!Error from HibernateUserDAO.setUserBean :save is not valid without active transaction

The followings are related files:

*hibernate.cfg.xml:
<hibernate-configuration>
<session-factory>

<property name="hibernate.session_factory_name">
java:comp/HibernateSessionFactory
</property>
<property name="hibernate.transaction.factory_class">
org.hibernate.transaction.JTATransactionFactory
</property>
<property name="hibernate.transaction.manager_lookup_class">
org.hibernate.transaction.WebSphereTransactionManagerLookup
</property>
<property name="jta.UserTransaction">
java:comp/UserTransaction
</property>


<property name="hibernate.connection.datasource">
jdbc/NA_DEV1
</property>



* HibernateUtil.java:
public static SessionFactory getSessionFactory() {
String sfName = configuration.getProperty(Environment.SESSION_FACTORY_NAME);
if ( sfName != null) {
//log.debug("Looking up SessionFactory in JNDI");
try {
return (SessionFactory) new InitialContext().lookup(sfName);
} catch (NamingException ex) {
throw new RuntimeException(ex);
}
} else if (sessionFactory == null) {
rebuildSessionFactory();
}
return sessionFactory;
}

...

*My code
public static void setUserBean(ExtranetUserH extranetUserH
) throws Exception {

UserTransaction tx = null;

try {

tx = (UserTransaction) new InitialContext()
.lookup("java:comp/UserTransaction");

tx.begin();


//get save is not valid without active transaction at below statement

HibernateUtil.getSessionFactory().getCurrentSession().save(extranetUserH);

tx.commit();
} catch (Exception e) {
....


The code works without using jndi for sessionfactory. Thanks in advance.

George
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


InitialContext did not implement EventContext

That error ir a bit of a red herring. Hibernate reports that error when the context used does not implement javax.naming.event.EventContext. All this will do is prevent Hibernate registering a naming listener, Hibernate itself should be working fine.

It soulds like Hibernate cannot look up JTA. Does WebSphere bind this in JNDI at java:comp/UserTransaction ? My memory is that in 5.1. it does not. JNDI in WebSphere is awkward full stop; again I remember that you needed to bootstrap some IBM classes unless you were running in an IBM JVM. I'm a bit hazy here - you might ask someone in the WebSphere forum to help?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
zuo liao
Greenhorn

Joined: Oct 11, 2007
Posts: 8
Hi Paul,

Thanks for your reply.

//This statement is OK.
Session session=HibernateUtil.getSessionFactory().getCurrentSession();

//I get "save is not valid without active transaction" error.
session.save(extranetUserH);

George
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Yep you always have to start a transaction when you open a Session.

Transaction transaction = session.getTransaction();
or was that Transaction transaction = session.beginTransaction();

They have to go hand in hand. a one to one relationship. And you have to make sure you commit your transaction when you want Hibernate to go to the database, when you are done (Well not exactly all the time, but for starting off we will say that). or rollback your transaction if an exception occurs.

transaction.commit(); or transaction.rollback();
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
zuo liao
Greenhorn

Joined: Oct 11, 2007
Posts: 8
Thanks Mark.


>They have to go hand in hand. a one to one relationship. And you have to make sure you commit your transaction when you want Hibernate to go to the database, when you are done (Well not exactly all the time, but for starting off we will say that). or rollback your transaction if an exception occurs.

>transaction.commit(); or transaction.rollback();


My code works without using jndi for the transaction:

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();

I do use transaction.commit(); and transaction.rollback();
 
jQuery in Action, 2nd edition
 
subject: InitialContext did not implement EventContext
 
Similar Threads
Hibernate: Why my log4j doesn't work in J2EE app?
glassfish vs. Hibernate: JNDI configuration
Hibernate Session Factories from org.springframework.orm.hibernate3.LocalSessionFactoryBean
Hibernate generate select while storing object.
Jboss/JTA/spring "No JTA TransactionManager found"