-->
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.  [ 13 posts ] 
Author Message
 Post subject: Get Hibernate to work with WebSphere 6.0
PostPosted: Thu Jan 31, 2008 3:45 pm 
Newbie

Joined: Thu Jan 31, 2008 3:26 pm
Posts: 3
Hi all,

I'm currently developing a Web application using WebSphere 6.0, Struts 1.3.9 and Hibernate 3.2.5.

I was managing the DB connection pool with Hibernate, but now I'm trying to manage it with a WebSphere datasource (DS). I'm using CMT Transactions.

The DS is properly configured (I think) and I can access to DB using Hibernate, but there is a problem:

If I use:

sessionFactory.openSession();

and then I use the session, it works fine, but if I try to use the following code:

Session session = sessionFactory.getCurrentSession();

An HibernateException is thrown:

org.hibernate.HibernateException: Current transaction is not in progress
at org.hibernate.context.JTASessionContext.currentSession(JTASessionContext.java:67)
at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:544)
at picage.model.dao.AdminUtilizadoresHome.findByExample(AdminUtilizadoresHome.java)
at picage.actions.AuthenticationAction.execute(AuthenticationAction.java:72)
at org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:58)
at org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:67)
at org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
at org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:304)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
at org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:763)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
(...)


The Hibernate.cfg.xml is the following:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="java:hibernate/PICAGESessionFactory">
<property name="hibernate.session_factory_name">PICAGESessionFactory</property>

<property name="hibernate.connection.datasource">jdbc/picageDs</property>
<property name="hibernate.dialect">picage.utils.PlatformOracleDialect</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.CMTTransactionFactory</property>
<property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.WebSphereExtendedJTATransactionLookup</property>
<property name="jta.UserTransaction">java:comp/UserTransaction</property>
<property name="hibernate.connection.pool_size">10</property>

<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">false</property>

<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="hibernate.connection.autocommit">true</property>
<property name="hibernate.current_session_context_class">jta</property>
<property name="hibernate.jdbc.use_streams_for_binary">true</property>
<property name="hibernate.jdbc.use_get_generated_keys">true</property>
<property name="hibernate.connection.autoReconnect">true</property>
<property name="hibernate.connection.autoReconnectForPools">true</property>
<property name="hibernate.connection.is-connection-validation-required">true</property>

<!-- Mappings... -->


Does any one know why I can't use getCurrentSession()?

Thanks


Cheers


Top
 Profile  
 
 Post subject: Re: Get Hibernate to work with WebSphere 6.0
PostPosted: Thu Jan 31, 2008 9:46 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
Because you are using a CMT transaction lookup but the call to currentSession is not within the scope of a transaction. Switching to a JTA transaction lookup might solve the problem (I know JTA transaction manager will start a transaction unless any transaction is present but I am not sure if this applies to call to currentSession). Alternatively you could get an instance of UserTransaction and start a transaction before calling currentSession. Spring will do the trick too.



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 01, 2008 6:40 am 
Newbie

Joined: Thu Jan 31, 2008 3:26 pm
Posts: 3
Hi,

Thank you for your answer, but I had already tried all that and it didn't work. I'm not using Spring, so that is not an option.

I think that the problem is that when I try to get (by hand) userTransaction from JNDI using "java:comp/UserTransaction" it does not work.

Do you know if this must be configured in WebSphere or Hibernate or somewhere else?


Thanks again :)

Luis


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 01, 2008 12:09 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
I don't quite understand what you mean when you get a user transaction it doesn't work. Do you get an exception? In addition, Spring should do it for you. What is your spring configuration?


Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 01, 2008 1:43 pm 
Newbie

Joined: Thu Jan 31, 2008 3:26 pm
Posts: 3
Hi,

I finally understood why I couldn't use getCurrentSession(). The problem is that there is no CMT in a web application. At least this was what I could find after several hours of google and hibernate research...

The solution, for me, was to use the following config in hibernate.cfg.xml:

<property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.WebSphereExtendedJTATransactionLookup</property>
<property name="jta.UserTransaction">java:comp/UserTransaction</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="hibernate.current_session_context_class">thread</property>

and then the getCurrentSession() worked just fine.


The only thing that I still can not understand with the previous config is:
why can I open a new session and in the next line of code, if I call getCurrentSession(), it throws an HibernateException?

Thanks.

Luis


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 01, 2008 1:47 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
Because open session just creates the objects. It doesn't really hook into anything before you actually need to use it. currentSession seeks for an open and bound-to-current-thread session.


Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 24, 2008 11:31 am 
Beginner
Beginner

Joined: Thu Nov 13, 2003 4:12 am
Posts: 27
Hi LMR, went exactly thru the same hell. There is some article on IBM Developworks

http://www.ibm.com/developerworks/websp ... lcott.html

which recommedns the CMTT Factory but obviosuly this is only working in an EJB module. The other option is to use bean managed transactions. These are the options:

#

for container-managed transactions:

<property name="hibernate.transaction.factory_class">
org.hibernate.transaction.CMTTransactionFactory
</property>
<property name="hibernate.transaction.manager_lookup_class">
org.hibernate.transaction.WebSphereExtendedJTATransactionLookup
</property>


#

for bean-managed transactions:

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

However, none of them works for me.
The only solution is to use

org.hibernate.transaction.JDBCTransactionFactory

as TRX factory.
Do you have any new insights, why this might be the case?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 24, 2008 12:39 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
RAPHEAD wrote:
Do you have any new insights, why this might be the case?



Is it a webapp or a standalone app?


Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 24, 2008 1:31 pm 
Beginner
Beginner

Joined: Thu Nov 13, 2003 4:12 am
Posts: 27
Hi,

It's a Web-App only, no EJB modules.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 24, 2008 2:22 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
RAPHEAD wrote:
Hi,

It's a Web-App only, no EJB modules.


It's a better idea to use JDBC transactions if database is the only transactional resource in your application.



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 24, 2008 2:50 pm 
Beginner
Beginner

Joined: Thu Nov 13, 2003 4:12 am
Posts: 27
sure, there is no need to use JDBC Tx management but I simply don't understand why it's not working... It is either a HB or a WAS issue.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 24, 2008 2:52 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
RAPHEAD wrote:
sure, there is no need to use JDBC Tx management but I simply don't understand why it's not working... It is either a HB or a WAS issue.



WebSphere does not implement standard transaction management technology so there are certain feature that are not available when working with WebSphere. If you still need to use a JTA manager then you might want to use a CMTTransactionFactory and make sure transaction are started before interacting with hibernate. That will work.




Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 24, 2008 3:14 pm 
Beginner
Beginner

Joined: Thu Nov 13, 2003 4:12 am
Posts: 27
I know WAS is a PITA when it comes to standards compliance... will stick with JDBC. thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 13 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.