-->
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.  [ 14 posts ] 
Author Message
 Post subject: Why need I have to begin transaction using "getCurrentS
PostPosted: Mon Aug 07, 2006 12:57 am 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.1.3

<hibernate-mapping>
<class name="lyo.test.User" table="user" catalog="mysql">
<comment></comment>
<composite-id name="id" class="lyo.test.UserId">
<key-property name="host" type="string">
<column name="Host" length="60" />
</key-property>
<key-property name="user" type="string">
<column name="User" length="16" />
</key-property>
</composite-id>
<property name="password" type="string">
<column name="Password" length="41" not-null="true">
<comment></comment>
</column>
</property>...........




Session s=HibernateUtil.getHibernateCurrentSession();//openSession();
//Transaction t=s.beginTransaction();
User u=(User)s.get(User.class, new UserId("sfd","sdf"));
System.out.println("Get: "+u);
//t.commit();


2006-08-07 12:43:22,225 DEBUG (SessionImpl.java:219) - opened session at timestamp: 4730576085872640
org.hibernate.HibernateException: get is not valid without active transaction
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:297)
at $Proxy0.get(Unknown Source)
at lyo.test.TestDB.main(TestDB.java:51)
Exception in thread "main"


MySQL 5.0


Version select: select Host, User from mysql.user where Host =? and User =?
2006-08-07 12:43:22,084 DEBUG (AbstractEntityPersister.java:2450) - Snapshot select: select user_.Host, user_.User, user_.Password as Password0_, user_.Select_priv as Select4_0_, user_.Insert_priv as Insert5_0_, user_.Update_priv as Update6_0_, user_.Delete_priv as Delete7_0_, user_.Create_priv as Create8_0_, user_.Drop_priv as Drop9_0_, user_.Reload_priv as Reload10_0_, user_.Shutdown_priv as Shutdown11_0_, user_.Process_priv as Process12_0_, user_.File_priv as File13_0_, user_.Grant_priv as Grant14_0_, user_.References_priv as References15_0_, user_.Index_priv as Index16_0_, user_.Alter_priv as Alter17_0_, user_.Show_db_priv as Show18_0_, user_.Super_priv as Super19_0_, user_.Create_tmp_table_priv as Create20_0_, user_.Lock_tables_priv as Lock21_0_, user_.Execute_priv as Execute22_0_, user_.Repl_slave_priv as Repl23_0_, user_.Repl_client_priv as Repl24_0_, user_.Create_view_priv as Create25_0_, user_.Show_view_priv as Show26_0_, user_.Create_routine_priv as Create27_0_, user_.Alter_routine_priv as Alter28_0_, user_.Create_user_priv as Create29_0_, user_.ssl_type as ssl30_0_, user_.ssl_cipher as ssl31_0_, user_.x509_issuer as x32_0_, user_.x509_subject as x33_0_, user_.max_questions as max34_0_, user_.max_updates as max35_0_, user_.max_connections as max36_0_, user_.max_user_connections as max37_0_ from mysql.user user_ where user_.Host=? and user_.User=?
2006-08-07 12:43:22,134 DEBUG (AbstractEntityPersister.java:2452) - Insert 0: insert into mysql.user (Password, Select_priv, Insert_priv, Update_priv, Delete_priv, Create_priv, Drop_priv, Reload_priv, Shutdown_priv, Process_priv, File_priv, Grant_priv, References_priv, Index_priv, Alter_priv, Show_db_priv, Super_priv, Create_tmp_table_priv, Lock_tables_priv, Execute_priv, Repl_slave_priv, Repl_client_priv, Create_view_priv, Show_view_priv, Create_routine_priv, Alter_routine_priv, Create_user_priv, ssl_type, ssl_cipher, x509_issuer, x509_subject, max_questions, max_updates, max_connections, max_user_connections, Host, User) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2006-08-07 12:43:22,134 DEBUG (AbstractEntityPersister.java:2453) - Update 0: update mysql.user set Password=?, Select_priv=?, Insert_priv=?, Update_priv=?, Delete_priv=?, Create_priv=?, Drop_priv=?, Reload_priv=?, Shutdown_priv=?, Process_priv=?, File_priv=?, Grant_priv=?, References_priv=?, Index_priv=?, Alter_priv=?, Show_db_priv=?, Super_priv=?, Create_tmp_table_priv=?, Lock_tables_priv=?, Execute_priv=?, Repl_slave_priv=?, Repl_client_priv=?, Create_view_priv=?, Show_view_priv=?, Create_routine_priv=?, Alter_routine_priv=?, Create_user_priv=?, ssl_type=?, ssl_cipher=?, x509_issuer=?, x509_subject=?, max_questions=?, max_updates=?, max_connections=?, max_user_connections=? where Host=? and User=?
2006-08-07 12:43:22,134 DEBUG (AbstractEntityPersister.java:2454) - Delete 0: delete from mysql.user where Host=? and User=?
2006-08-07 12:43:22,165 DEBUG (EntityLoader.java:79) - Static select for entity lyo.test.User: select user0_.Host as Host0_0_, user0_.User as User0_0_, user0_.Password as Password0_0_, user0_.Select_priv as Select4_0_0_, user0_.Insert_priv as Insert5_0_0_, user0_.Update_priv as Update6_0_0_, user0_.Delete_priv as Delete7_0_0_, user0_.Create_priv as Create8_0_0_, user0_.Drop_priv as Drop9_0_0_, user0_.Reload_priv as Reload10_0_0_, user0_.Shutdown_priv as Shutdown11_0_0_, user0_.Process_priv as Process12_0_0_, user0_.File_priv as File13_0_0_, user0_.Grant_priv as Grant14_0_0_, user0_.References_priv as References15_0_0_, user0_.Index_priv as Index16_0_0_, user0_.Alter_priv as Alter17_0_0_, user0_.Show_db_priv as Show18_0_0_, user0_.Super_priv as Super19_0_0_, user0_.Create_tmp_table_priv as Create20_0_0_, user0_.Lock_tables_priv as Lock21_0_0_, user0_.Execute_priv as Execute22_0_0_, user0_.Repl_slave_priv as Repl23_0_0_, user0_.Repl_client_priv as Repl24_0_0_, user0_.Create_view_priv as Create25_0_0_, user0_.Show_view_priv as Show26_0_0_, user0_.Create_routine_priv as Create27_0_0_, user0_.Alter_routine_priv as Alter28_0_0_, user0_.Create_user_priv as Create29_0_0_, user0_.ssl_type as ssl30_0_0_, user0_.ssl_cipher as ssl31_0_0_, user0_.x509_issuer as x32_0_0_, user0_.x509_subject as x33_0_0_, user0_.max_questions as max34_0_0_, user0_.max_updates as max35_0_0_, user0_.max_connections as max36_0_0_, user0_.max_user_connections as max37_0_0_ from mysql.user user0_ where user0_.Host=? and user0_.User=?
2006-08-07 12:43:22,165 DEBUG (EntityLoader.java:79) - Static select for entity lyo.test.User: select user0_.Host as Host0_0_, user0_.User as User0_0_, user0_.Password as Password0_0_, user0_.Select_priv as Select4_0_0_, user0_.Insert_priv as Insert5_0_0_, user0_.Update_priv as Update6_0_0_, user0_.Delete_priv as Delete7_0_0_, user0_.Create_priv as Create8_0_0_, user0_.Drop_priv as Drop9_0_0_, user0_.Reload_priv as Reload10_0_0_, user0_.Shutdown_priv as Shutdown11_0_0_, user0_.Process_priv as Process12_0_0_, user0_.File_priv as File13_0_0_, user0_.Grant_priv as Grant14_0_0_, user0_.References_priv as References15_0_0_, user0_.Index_priv as Index16_0_0_, user0_.Alter_priv as Alter17_0_0_, user0_.Show_db_priv as Show18_0_0_, user0_.Super_priv as Super19_0_0_, user0_.Create_tmp_table_priv as Create20_0_0_, user0_.Lock_tables_priv as Lock21_0_0_, user0_.Execute_priv as Execute22_0_0_, user0_.Repl_slave_priv as Repl23_0_0_, user0_.Repl_client_priv as Repl24_0_0_, user0_.Create_view_priv as Create25_0_0_, user0_.Show_view_priv as Show26_0_0_, user0_.Create_routine_priv as Create27_0_0_, user0_.Alter_routine_priv as Alter28_0_0_, user0_.Create_user_priv as Create29_0_0_, user0_.ssl_type as ssl30_0_0_, user0_.ssl_cipher as ssl31_0_0_, user0_.x509_issuer as x32_0_0_, user0_.x509_subject as x33_0_0_, user0_.max_questions as max34_0_0_, user0_.max_updates as max35_0_0_, user0_.max_connections as max36_0_0_, user0_.max_user_connections as max37_0_0_ from mysql.user user0_ where user0_.Host=? and user0_.User=?
2006-08-07 12:43:22,165 DEBUG (EntityLoader.java:79) - Static select for entity lyo.test.User: select user0_.Host as Host0_0_, user0_.User as User0_0_, user0_.Password as Password0_0_, user0_.Select_priv as Select4_0_0_, user0_.Insert_priv as Insert5_0_0_, user0_.Update_priv as Update6_0_0_, user0_.Delete_priv as Delete7_0_0_, user0_.Create_priv as Create8_0_0_, user0_.Drop_priv as Drop9_0_0_, user0_.Reload_priv as Reload10_0_0_, user0_.Shutdown_priv as Shutdown11_0_0_, user0_.Process_priv as Process12_0_0_, user0_.File_priv as File13_0_0_, user0_.Grant_priv as Grant14_0_0_, user0_.References_priv as References15_0_0_, user0_.Index_priv as Index16_0_0_, user0_.Alter_priv as Alter17_0_0_, user0_.Show_db_priv as Show18_0_0_, user0_.Super_priv as Super19_0_0_, user0_.Create_tmp_table_priv as Create20_0_0_, user0_.Lock_tables_priv as Lock21_0_0_, user0_.Execute_priv as Execute22_0_0_, user0_.Repl_slave_priv as Repl23_0_0_, user0_.Repl_client_priv as Repl24_0_0_, user0_.Create_view_priv as Create25_0_0_, user0_.Show_view_priv as Show26_0_0_, user0_.Create_routine_priv as Create27_0_0_, user0_.Alter_routine_priv as Alter28_0_0_, user0_.Create_user_priv as Create29_0_0_, user0_.ssl_type as ssl30_0_0_, user0_.ssl_cipher as ssl31_0_0_, user0_.x509_issuer as x32_0_0_, user0_.x509_subject as x33_0_0_, user0_.max_questions as max34_0_0_, user0_.max_updates as max35_0_0_, user0_.max_connections as max36_0_0_, user0_.max_user_connections as max37_0_0_ from mysql.user user0_ where user0_.Host=? and user0_.User=? for update
2006-08-07 12:43:22,165 DEBUG (EntityLoader.java:79) - Static select for entity lyo.test.User: select user0_.Host as Host0_0_, user0_.User as User0_0_, user0_.Password as Password0_0_, user0_.Select_priv as Select4_0_0_, user0_.Insert_priv as Insert5_0_0_, user0_.Update_priv as Update6_0_0_, user0_.Delete_priv as Delete7_0_0_, user0_.Create_priv as Create8_0_0_, user0_.Drop_priv as Drop9_0_0_, user0_.Reload_priv as Reload10_0_0_, user0_.Shutdown_priv as Shutdown11_0_0_, user0_.Process_priv as Process12_0_0_, user0_.File_priv as File13_0_0_, user0_.Grant_priv as Grant14_0_0_, user0_.References_priv as References15_0_0_, user0_.Index_priv as Index16_0_0_, user0_.Alter_priv as Alter17_0_0_, user0_.Show_db_priv as Show18_0_0_, user0_.Super_priv as Super19_0_0_, user0_.Create_tmp_table_priv as Create20_0_0_, user0_.Lock_tables_priv as Lock21_0_0_, user0_.Execute_priv as Execute22_0_0_, user0_.Repl_slave_priv as Repl23_0_0_, user0_.Repl_client_priv as Repl24_0_0_, user0_.Create_view_priv as Create25_0_0_, user0_.Show_view_priv as Show26_0_0_, user0_.Create_routine_priv as Create27_0_0_, user0_.Alter_routine_priv as Alter28_0_0_, user0_.Create_user_priv as Create29_0_0_, user0_.ssl_type as ssl30_0_0_, user0_.ssl_cipher as ssl31_0_0_, user0_.x509_issuer as x32_0_0_, user0_.x509_subject as x33_0_0_, user0_.max_questions as max34_0_0_, user0_.max_updates as max35_0_0_, user0_.max_connections as max36_0_0_, user0_.max_user_connections as max37_0_0_ from mysql.user user0_ where user0_.Host=? and user0_.User=? for update


DEBUG

Hi friends:

When I use the code :
Code:

Session s=HibernateUtil.getHibernateCurrentSession();//openSession();
            User u=(User)s.get(User.class, new UserId("sfd","sdf"));
      System.out.println("Get: "+u);
      

I get a exception:
Code:
org.hibernate.HibernateException: get is not valid without active transaction


I know what it is meaning, it means that if I want to use "getCurrentSession()" I need have to begin a transaction . But the only thing I want to do is a simple query not insert or update. I don't want to begin a transaction when I do a simple query(Not insert or Update!). I think it is waste memory.

If I don't want to use transaction,how should I do it? Thks!

_________________
You are not alone...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 07, 2006 1:13 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
running a simple query always creates a transaction at the jdbc level....there is no waste of memory.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: :(
PostPosted: Mon Aug 07, 2006 1:31 am 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
max wrote:
running a simple query always creates a transaction at the jdbc level....there is no waste of memory.


But it is a time waste, it need more time to begin a transaction than do a simple query. In jdbc programming, I don't need have to using transaction when I do such a query,like this:
Code:

String sql="select * from User";
      Class.forName("com.mysql.jdbc.Driver");
      Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root","test");
      Statement stm=con.createStatement();
      ResultSet rs=stm.executeQuery(sql);
      while(rs.next()){
         System.out.println("Get: "+rs.getString("Host"));
         
      }



It don't need have to begin a transaction,is it true?

_________________
You are not alone...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 07, 2006 1:40 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
oh well small correction:

there is always created a transaction at the level of database.

Go read up about it in Hibernate In Action or just about transactions in general.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 07, 2006 1:41 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
oh well small correction:

there is always created a transaction at the level of database.

Go read up about it in Hibernate In Action or just about transactions in general.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: :)
PostPosted: Mon Aug 07, 2006 1:52 am 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
max wrote:
oh well small correction:

there is always created a transaction at the level of database.

Go read up about it in Hibernate In Action or just about transactions in general.


You mean that jdbc do it(transaction) in background? So I don't need have to code it every time I do a query. Thank you! I will see it.

_________________
You are not alone...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 07, 2006 2:34 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
no, i mean that your wish for running without a transaction is not possible since it is either done implicitly by the database or explicitly by you.

Thus doing it implicitly doesn't save you from anything, it is actually the opposite since it will now have one tx per query instead of one tx for many queries.

And when you are using currentsessionctx we need the explicit tx boundaries to have a entry point for setting up and closing the session.....

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: :(
PostPosted: Mon Aug 07, 2006 3:10 am 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
max wrote:
no, i mean that your wish for running without a transaction is not possible since it is either done implicitly by the database or explicitly by you.

Thus doing it implicitly doesn't save you from anything, it is actually the opposite since it will now have one tx per query instead of one tx for many queries.

And when you are using currentsessionctx we need the explicit tx boundaries to have a entry point for setting up and closing the session.....


But Hibernate is a encapsulation of jdbc. So why it couldn't do it in background? I mean that why hibernate don't do it itself ?

_________________
You are not alone...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 07, 2006 3:16 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
from http://www.hibernate.org/42.html

Transactions

Transactions also group data access operations, in fact, every SQL statement, be it queries or DML, has to execute inside a database transaction. There can be no communication with a database outside of a database transaction. (Note that there are such things as read-only transactions, that can be used to improve cleanup time in a database engine if it is not smart enough to optimize its own operations.)

One approach is the auto-commit mode, where every single SQL statement is wrapped in a very short transaction. This mode is never appropriate for an application, but only for ad-hoc execution of SQL with an operator console. Hibernate disables or expects the environment (in J2EE/JEE) to disable auto-commit mode, as applications are not executing ad-hoc SQL but a planned sequence of statements. (There are ways to enable auto-commit behavior in Hibernate but it is by definition slower than regular transactions and less safe.)

The right approach is to define clear transaction boundaries in your application by beginning and committing transactions either programmatically, or if you have the machinery to do this, declaratively (e.g. on service/command methods). If an exception occurs the transaction has to be rolled back (or declaratively, is rolled back).

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: :)
PostPosted: Mon Aug 07, 2006 8:48 pm 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
max wrote:
There are ways to enable auto-commit behavior in Hibernate but it is by definition slower than regular transactions and less safe.


Thank you for your reply.
But auto-commit behavior is slower than regular transactions ? It is different with my mind. I think auto-commit is the fast database operation.

_________________
You are not alone...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 08, 2006 12:49 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
well, that is your choice to belive that ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 08, 2006 4:58 am 
Regular
Regular

Joined: Wed Sep 28, 2005 6:45 pm
Posts: 56
The same as Max said, but more verbose:

If you have 5 queries running each in its own transaction, this is what the database does:

begin transaction
run query
end transaction
begin transaction
run query
end transaction
begin transaction
run query
end transaction
begin transaction
run query
end transaction
begin transaction
run query
end transaction

if you run them in the same transaction, this is what the database does:
begin transaction
run query
run query
run query
run query
run query
end transaction

In other words, the database only has to begin/end one transaction istead of five. Its faster, just as Max said ;)

In the last instance you might even manage to run a batch query instead of five single selects on the jdbc level, in which case you get on database round trip, instead of 5 which would be even faster.

If you don't want to manage the transactions yourself you could try using for instance stateless session beans and an application server like JBoss. That way you could just write the query without thinking about managing the transactions. (which is kinda the point of using that approach).


Top
 Profile  
 
 Post subject: :)
PostPosted: Tue Aug 08, 2006 11:12 pm 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
Blackbrrd wrote:
The same as Max said, but more verbose:

If you have 5 queries running each in its own transaction, this is what the database does:
......................................................

If you don't want to manage the transactions yourself you could try using for instance stateless session beans and an application server like JBoss. That way you could just write the query without thinking about managing the transactions. (which is kinda the point of using that approach).


Thank you.Your example is useful for me. I will try it by code ;-)

_________________
You are not alone...


Top
 Profile  
 
 Post subject: :)
PostPosted: Tue Aug 08, 2006 11:14 pm 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
max wrote:
well, that is your choice to belive that ;)


Thank you max! And Blackbrrd 's database example is good for me too ;-)

_________________
You are not alone...


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