• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

InitialContext did not implement EventContext

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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?
 
zuo liao
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
zuo liao
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
 
She's out of the country right now, toppling an unauthorized dictatorship. Please leave a message with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic