-->
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.  [ 9 posts ] 
Author Message
 Post subject: Transaction problem...
PostPosted: Tue May 10, 2005 8:28 am 
Regular
Regular

Joined: Fri Feb 25, 2005 3:02 am
Posts: 71
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

[b]Hibernate version:3.0[/b]

[b]Mapping documents:[/b]

[b]Code between sessionFactory.openSession() and session.close():
/////////////////////////////--- query 1 ---////////////////////////
Session aSession = null;
//Transaction tx = null;
try
{
aSession = HibernateUtil.currentSession();
//tx = aSession.beginTransaction();
Query q = aSession.createQuery("FROM Customer c order by c.name");
out.println("SIZE1: " + q.list().size());
//tx.commit();
}
catch (Exception e)
{
//if (tx != null)
//tx.rollback();
}
finally
{
out.println("closing hibernate session...<br>");
HibernateUtil.closeSession();

if (aSession.isOpen())
out.println("ERROR : hibernate session still open !<br>");
}
/////////////////////////////--- end query 1 ---////////////////////////

/////////////////////////////--- query 2 ---////////////////////////
Session bSession = null;
//Transaction tx1 = null;
try
{
currSession = HibernateUtil.currentSession();
//tx1 = currSession.beginTransaction();
Query query = currSession.createQuery("FROM Customer c order by c.name");
//out.println("SIZE2: " + query.list().size());
//tx1.commit();
}
catch (Exception e)
{
//if (tx1 != null)
//tx1.rollback();
}
finally
{
out.println("closing hibernate session...<br>");
HibernateUtil.closeSession();

if (currSession.isOpen())
out.println("ERROR : hibernate session still open !<br>");
}
/////////////////////////////--- end query 2 ---////////////////////////

[/b]

[b]Full stack trace of any exception that occurs:[/b]

[b]Name and version of the database you are using:postgresql 8.0[/b]

[b]The generated SQL (show_sql=true):[/b]

[b]Debug level Hibernate log excerpt:[/b]

Hi, I would like to know if any database access requires a transaction. Am asking this because, when I execute the above 2 queries (WITHOUT ANY TRANSACTION) in one call, I found that there is an 'idle in transaction' in my postgresql log. As if the transaction has not been commited. But I did not start any transaction. Can anyone plese tell me what is causing this? An when I execute the same 2 queries but now I put transaction in each of them, there is no 'idle in transaction'. I don't undestand this. Am using Hibernate in a non-managed environment (using Tomcat).

Can anyone please explain me why I got this 'idle in transaction'
thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 10, 2005 10:21 am 
Regular
Regular

Joined: Mon Jul 26, 2004 2:28 pm
Posts: 86
Location: Pensacola, Florida
I don't know for sure, but I'll take a stab at it...

In JDBC there is no explicit method call to begin a transaction. By virtue of opening a connection you are in effect starting a transaction. The semantics of that transaction are governed by the setAutoCommit( ) method. If autoCommit is false, then the transaction is ended with an explicit call to commit( ) or rollback( ). If autoCommit is true, then the transaction is committed implicitly at the end of each statement. I imagine that a transaction is ended implicitly on close( ) in either case.

A Hibernate session wraps a JDBC connection, and, I think, sets autoCommit to false. As the session is flushed, JDBC statements are executed and sent to the database in the context of a transaction. When Transaction.commit( ) is called, a commit( ) is executed on the JDBC connection (unless you are using user-specified connections, in which case an explicit session.connection( ).commit( ) call is required).

My suspicion is that when you do not use a transaction, the connection is opened and set to autoCommit="false". At that point the database expects an explicit commit( ) eventually. But since you are not committing the transaction, the database never gets a commit( ), and it outputs an idle transaction warning.

In any case, based on what I've read on the board and in HiA, it's always recommended that you use a Transaction. The examples in HiA use transactions, and they are mostly read-only operations.

- Jesse


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 11, 2005 12:45 am 
Regular
Regular

Joined: Fri Feb 25, 2005 3:02 am
Posts: 71
Hi jesse, thank you very much for the explanation. But if I don't use a transaction how would I call the commit() method?


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 11, 2005 3:41 pm 
Regular
Regular

Joined: Mon Jul 26, 2004 2:28 pm
Posts: 86
Location: Pensacola, Florida
session.connection( ).commit( );


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 12, 2005 12:51 am 
Regular
Regular

Joined: Fri Feb 25, 2005 3:02 am
Posts: 71
Hi jesse, in your first reply, you told me the following:

[i]In JDBC there is no explicit method call to begin a transaction[/i]

But what about session.beginTransaction()? Doesn't this start a new transaction if you call this method in anon-managed environment?

As far as I know I think that this will create a new transaction. PLease correct me if am wrong.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 12, 2005 1:33 pm 
Regular
Regular

Joined: Mon Jul 26, 2004 2:28 pm
Posts: 86
Location: Pensacola, Florida
In JDBC new transactions are created implicitly when the connection is made. Commits can happen two ways: implicitly at the end of each statement (autoCommit=true) or explicitly with Connection.commit( )(autoCommit=false). Session.beginTransaction( ) sets up a Hibernate transaction (as opposed to a JDBC transaction), which has other semantics.

- Jesse


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 13, 2005 12:46 am 
Regular
Regular

Joined: Fri Feb 25, 2005 3:02 am
Posts: 71
thanks again for your explanation jesse


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 13, 2005 9:12 am 
Regular
Regular

Joined: Fri Feb 25, 2005 3:02 am
Posts: 71
Sorry to annoy you once more jesse, I have now changed all my codes so that I use 'session.connection().commit()' instead of doing:

tx = session.beginTransaction();
........
......
tx.commit();

The good thing is that there is no more 'idle in transaction' in my postgresql log, but now I found that only my java objects are being modified, the data in the database don't get updated!

What could be the problem?


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 13, 2005 11:31 am 
Newbie

Joined: Fri May 13, 2005 11:16 am
Posts: 1
gulshan wrote:
Sorry to annoy you once more jesse, I have now changed all my codes so that I use 'session.connection().commit()' instead of doing:

tx = session.beginTransaction();
........
......
tx.commit();

The good thing is that there is no more 'idle in transaction' in my postgresql log, but now I found that only my java objects are being modified, the data in the database don't get updated!

What could be the problem?


Maybe the "transparent" JDBC transaction doesn't see the Hibernate-level cached updates.

I had the same problem just till now with SQLServer and the solution has been to use:

Code:
session.beginTransaction().commit();


instead of

Code:
session.getConnection().commit();


It should start the transaction and atomically update the data on the db.

It worked fine with me. Hope it helps!

Ivano.


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