-->
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.  [ 12 posts ] 
Author Message
 Post subject: Problems with JTA, please help me
PostPosted: Sun Mar 12, 2006 10:15 am 
Beginner
Beginner

Joined: Sun Mar 05, 2006 12:16 am
Posts: 31
Hibernate version:
3.1.2

Mapping documents:
hibernate.cfg.xml
<?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="SessionFactory">
<property name="hibernate.cglib.use_reflection_optimizer">true</property>
<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.connection.password">30b12a87</property>
<property name="hibernate.connection.url">jdbc:mysql:///kdc</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.current_session_context_class">jta</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.jta.UserTransaction">UserTransaction</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
<property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>
<mapping resource="com/kdccn/general/entity/User.hbm.xml" />
</session-factory>
</hibernate-configuration>

Code between sessionFactory.openSession() and session.close():
org.hibernate.Session s;
Configuration cfg = new Configuration().configure();
org.hibernate.SessionFactory sf=cfg.buildSessionFactory();
s = sf.openSession();
UserTransaction tx=(UserTransaction)new InitialContext().lookup("UserTransaction");
tx.begin();
org.hibernate.Session s1=sf.getCurrentSession();
User user = new User();
user.setUsername("Someone");
user.setPassword("guessme");
s1.save(user);
tx.commit();
s.close();

The problem is that it seems that the commit() failed. I try to check the rows in the MySQL Administrator. It showed that the rows is 1. But after I select the results, it returns none. I thought it is the problem with jta.


Last edited by Seto on Fri Mar 17, 2006 7:05 am, edited 3 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 12, 2006 10:25 am 
Beginner
Beginner

Joined: Sun Mar 05, 2006 12:16 am
Posts: 31
Can someone give me some sucessful example with JTA


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 17, 2006 7:07 am 
Beginner
Beginner

Joined: Sun Mar 05, 2006 12:16 am
Posts: 31
please help me, this problem disturb me very much


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 28, 2006 1:03 pm 
Beginner
Beginner

Joined: Thu Jun 30, 2005 4:20 am
Posts: 40
Location: Vienna
Hi,

I do not think that you can use JDBC directly with JTA (but I may be wrong here).

So instead of the following lines:
Code:
<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.connection.password">30b12a87</property>
<property name="hibernate.connection.url">jdbc:mysql:///kdc</property>
<property name="hibernate.connection.username">root</property>


you should give your connection datasource, e.g.:
Code:
<property name="connection.datasource">java:comp/env/jdbc/rzbrss_oratgr_xa</property>

Of course you also will have to configure your datasource in your appserver.

maybe this helps...

best regards
Stefan


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 28, 2006 2:06 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
whether you run in a managed (ejb) env or not (ie webserver), hibernate makes transactions transparent to the user... in other words, you should just call

tx=sess.beginTransaction();
sess.save(...);
tx.commit();

If it is in a managed env, then the JTA will be used, else a simple jdbc transaction is used.

_________________
-JT

If you find my replies helpful, please rate by clicking 'Y' on them. I appreciate it.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 3:07 am 
Beginner
Beginner

Joined: Thu Jun 30, 2005 4:20 am
Posts: 40
Location: Vienna
Unfortunately, this is not true, because the getCurrentSession() functions expects a running JTA Transaction when it is called. According to the Hibernate reference, chapter 11.2.2 "Using JTA", you have to use the JTA API directly...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 12:33 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
stefanm wrote:
According to the Hibernate reference, chapter 11.2.2 "Using JTA", you have to use the JTA API directly...


http://www.hibernate.org/hib_docs/v3/reference/en/pdf/hibernate_reference.pdf

OK - so did you read 11.2.2? It states exactly what I said. Where does it say that you need to use the JTA API directly... I could not find it...

I quote two sources from the reference.pdf, that support my statements:

3.8.3. Current Session context management with JTA
Quote:
Your code can either use JTA programmatically through UserTransaction, or (recommended for portable code) use the Hibernate Transaction API to set transaction boundaries.


From 11.2.2. Using JTA
Quote:
If your persistence layer runs in an application server (e.g. behind EJB session beans), every datasource connection
obtained by Hibernate will automatically be part of the global JTA transaction. Hibernate offers two strategies for this integration.

If you use bean-managed transactions (BMT) Hibernate will tell the application server to start and end a BMT
transaction if you use the Transaction API. So, the transaction management code is identical to the nonmanaged
environment.


Again..."So, the transaction management code is identical to the nonmanaged environment."

_________________
-JT

If you find my replies helpful, please rate by clicking 'Y' on them. I appreciate it.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 12:42 pm 
Beginner
Beginner

Joined: Thu Jun 30, 2005 4:20 am
Posts: 40
Location: Vienna
In 11.2.2 it says:

Quote:
If you want to use a transaction-bound Session, that is, the getCurrentSession() functionality for easy context
propagation, you will have to use the JTA UserTransaction API directly:


So in my opinion, what you say is only true for as long as you do not use getCurrentSession()... Anyway, I agree with you that even with getCurrentSession(), it should be possible to use the Hibernate Transaction API. But the problem is that the Transaction must be started before you call getCurrentSession(), so I do not see how it would be possible as you neet the session to start a transaction using the Hibernate Transaction API.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 12:48 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
I am looking at the reference.pdf for hibernate 3.1, as referenced in the URL that I gave, and I don't see your quote at all? Perhaps this has changed from previous versions.?? What doc are you looking at?

_________________
-JT

If you find my replies helpful, please rate by clicking 'Y' on them. I appreciate it.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 1:03 pm 
Beginner
Beginner

Joined: Thu Jun 30, 2005 4:20 am
Posts: 40
Location: Vienna
I looked at the reference pdf for Hibernate 3.1.2, but it is also in the link you sent: page 127, approximately in the middle.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 1:21 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
interesting - the 3.1 docs that are online are different than the 3.1 docs that were distributed with 3.1.1 zip file... OK so now that we are on the same page, I interpret that to mean that if you want your entire "Session" under the control of the transaction, vs transactions within a session.

eg.
JTA managed transaction
session
work
session.close()
JTA ending trx.

vs.

session.beginTransaction()
work....
tx.commit()

which should still work with JTA in the managed env.

Does that sound right to you too?

_________________
-JT

If you find my replies helpful, please rate by clicking 'Y' on them. I appreciate it.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 13, 2006 9:00 am 
Newbie

Joined: Thu Mar 09, 2006 7:02 am
Posts: 6
Location: Kiev
I had the same problem. Looks like save(insert to table) doesn't works with JTA transactions : Hibernate + JOTM + Tomcat.

But then I configured Datasource in Tomcat and it works !!!!
remove this:

<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.connection.password">30b12a87</property>
<property name="hibernate.connection.url">jdbc:mysql:///kdc</property>
<property name="hibernate.connection.username">root</property>

put this:

<property name="connection.datasource">java:comp/env/jdbc/rzbrss_oratgr_xa</property>

_________________
Oleksandr Markelov
SCJP, SCWCD, SCBCD


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