-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 
Author Message
 Post subject: WebSphere and Hibernate integration
PostPosted: Thu Sep 09, 2004 12:00 am 
Newbie

Joined: Mon Sep 06, 2004 8:49 am
Posts: 7
Is there any specific setting that needs to be done to integrate Hibernate with WebSphere.
I am using WAS 5.0 and hibernate 2.1.2. I tried creating/updating data using a Web application and using only a Session bean. The callout using a web application worked and but for some reason callout using purely session bean is not working.

In both the cases, data retrieval happens but create /update fails in case of a pure session bean call. There are no exception messages generated. I am trying to use the Application server's JTA but for some reason hibernate does not seem to be participating in the application server initiated transaction.

Could some one give me pointers to fix this problem?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 09, 2004 1:57 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
http://www.hibernate.org/hib_docs/reference/en/html_single/#configuration-optional
Pay attention when the transaction word is used.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 10, 2004 12:22 am 
Newbie

Joined: Mon Sep 06, 2004 8:49 am
Posts: 7
Hi Emmanuel,

Thanks for the reply. Let me explain my problem with a sample example. I am using hibernate for persistence within WebSphere Application Server. There is no Web application in the EAR only a session bean ejb jar is available.

Following are some transactional property setting present in the hibernate.cfg.xml:

<property name="connection.datasource">java:comp/env/jdbc/ltcadb</property>
<property name="hibernate.connection.pool_size">20</property>
<property name="dialect">net.sf.hibernate.dialect.OracleDialect</property>
<property name="show_sql">true</property>
<property name="transaction.factory_class">
net.sf.hibernate.transaction.JTATransactionFactory
</property>
<property name="transaction.manager_lookup_class">
net.sf.hibernate.transaction.WebSphereTransactionManagerLookup
</property>
<property name="hibernate.cache.provider_class">
net.sf.hibernate.cache.HashtableCacheProvider
</property>
<property name="jndi.class">com.ibm.websphere.naming.WsnInitialContextFactory</property>
<property name="jndi.url">iiop://localhost:2809/</property>

A method loadparents is defined in a Session bean with TX attribute Required.

The method is as follows:
public void loadParents(){
Session session = HibernateUtil.currentSession();
try {
Transaction t = session.beginTransaction();
List l = session.find(" FROM Parent p WHERE 1=1");

int parentCount = l.size();
for(int i=0; i<parentCount; i++){
Parent p = (Parent)l.get(i);
p.setMiddleName("CHG123");
System.out.println("Parent: " + i+"--");
System.out.println(p);
session.update(p);
}
t.commit();
session.close();
} catch(HibernateException hibEx){
hibEx.printStackTrace();
}
}

Running the above session bean resulted in a NameNotFoundException.

Please find below the snippet of the exception stack trace:
[9/10/04 9:37:49:268 IST] 7c7a9217 JTATransactio E net.sf.hibernate.transaction.JTATransaction Could not find UserTransaction in JNDI
[9/10/04 9:37:49:308 IST] 7c7a9217 JTATransactio E net.sf.hibernate.transaction.JTATransaction TRAS0014I: The following exception was logged javax.naming.NameNotFoundException: Name "comp/UserTransaction" not found in context "java:".
at com.ibm.ws.naming.ipbase.NameSpace.lookupInternal(NameSpace.java:1021)
at com.ibm.ws.naming.ipbase.NameSpace.lookup(NameSpace.java:932)
at com.ibm.ws.naming.urlbase.UrlContextImpl.lookup(UrlContextImpl.java:1261)
at com.ibm.ws.naming.java.javaURLContextRoot.lookup(javaURLContextRoot.java:196)
at com.ibm.ws.naming.java.javaURLContextRoot.lookup(javaURLContextRoot.java:137)
at javax.naming.InitialContext.lookup(InitialContext.java:360)
at net.sf.hibernate.transaction.JTATransaction.begin(JTATransaction.java:132)
at net.sf.hibernate.transaction.JTATransactionFactory.beginTransaction(JTATransactionFactory.java:58)
at net.sf.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:2195)
at com.vinraj.rd.hibernate.session.CalloutBean.loadParents(CalloutBean.java:93)
at com.vinraj.rd.hibernate.session.EJSLocalStatelessCallout_2a04f665.loadParents(EJSLocalStatelessCallout_2a04f665.java:79)

I understand that this is a valid exception as java:comp/UserTransaction lookup is possible only in case of WebApps not in case of EJB applications, where normally if you need a handle to the UserTransaction it is obtained using *Context.getUserTransaction().

I commented out the portion of code starting and committing transaction.

Transaction t = session.beginTransaction();
t.commit();

Hoping that the transaction start and commit would be the EJB container's responsibility. The code ran fine but the data did not get updated.

Could you please help me out in fixing this problem. Are there any other areas that I have missed?

Thanks,
Vinay


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 10, 2004 4:48 am 
Beginner
Beginner

Joined: Thu May 06, 2004 5:30 am
Posts: 45
just use <property name="connection.datasource">jdbc/ltcadb</property>
for your datasource instead of java:comp/...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 10, 2004 6:11 am 
Newbie

Joined: Mon Sep 06, 2004 8:49 am
Posts: 7
The change suggested in connection.datasource did not work. Any other ideas?
thanks,
Vinay


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 10, 2004 9:37 am 
Senior
Senior

Joined: Fri May 14, 2004 9:37 am
Posts: 122
Location: Cologne, Germany
Set the property jta.UserTransaction to it's correct JNDI name for your app server

hibernate.transaction.factory_class The classname of a TransactionFactory to use with Hibernate Transaction API (defaults to JDBCTransactionFactory).

eg. classname.of.TransactionFactory
jta.UserTransaction A JNDI name used by JTATransactionFactory to obtain the JTA UserTransaction from the application server.

eg. jndi/composite/name

May be by default java:/comp/UserTransaction is taken.

_________________
regards

Olaf

vote if it helped


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 10, 2004 9:45 am 
Senior
Senior

Joined: Fri May 14, 2004 9:37 am
Posts: 122
Location: Cologne, Germany
It should be java:comp/UserTransaction without slash.
so add to hibernate.cfg.xml
<property name="jta.UserTransaction">java:comp/UserTransaction</property>
hope this lets the exception disappear.

_________________
regards

Olaf

vote if it helped


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 13, 2004 12:26 am 
Newbie

Joined: Mon Sep 06, 2004 8:49 am
Posts: 7
I have set the property jta.UserTransaction correctly in the hibernate.cfg.xml.

The exception appears if I have a session.beginTransaction in my code. Since I plan to use the Websphere provided container managed transaction handling, I believe I do not have to write the session.beginTransaction or session.commit(Details already explained in my previous post.)

When I run the code without the beginTransaction/Commit portion, no exceptions are generated but any create/update operations carried out do not get committed to database. Note that all read operations run fine.

Any pointers to fix the problem are welcome.

Regards,
Vinay


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 13, 2004 12:59 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
flush() the session ... this is a FAQ


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 13, 2004 1:36 am 
Newbie

Joined: Mon Sep 06, 2004 8:49 am
Posts: 7
Thanks Gavin. That solved the problem.

Regards,
vinay


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.