-->
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: hibernate in jboss, How can I set transaction level?
PostPosted: Mon Aug 21, 2006 11:14 pm 
Newbie

Joined: Mon Aug 21, 2006 9:15 pm
Posts: 17
I know in sql92 standard, any database should provide four transaction level: read uncommitted, read commit, read repeatable, serializable.
and also jdbc can provide this support.
but when merging into container, when using container managed transaction, transaction level default to be read commit, but How can I set it to any other level?
and hibernate resolve difference between many popular database such as db2,oracle etc. in traditional jdbc programming, I like to use database relative sql statement to change transaction level(like in oracle, I execute in Statement: "set transaction read only" to set serializable isolation level). then I don't know how to set this in Hibernate?


Top
 Profile  
 
 Post subject: Re: setting isolation levels
PostPosted: Tue Aug 22, 2006 12:16 am 
Newbie

Joined: Wed Aug 16, 2006 6:29 pm
Posts: 9
You can set it by changing the configuratin of the underlying database or by specifying the transaction isolation level for JDBC connections in Hibernate properties:

Quote:
hibernate.connection.isolation = 4


Note: check java.sql.Connection for meaningful values (1, 2, 4, 8).. 1 is Read Uncommitted, 8 is Serializable.


Top
 Profile  
 
 Post subject: thanks for your reply
PostPosted: Tue Aug 22, 2006 12:44 am 
Newbie

Joined: Mon Aug 21, 2006 9:15 pm
Posts: 17
thank you.
I'd like to set this in program. because either set in database or set in hibernate configuration file is not suitable for me. it will make all operation use this transaction level. but I just want to use it in some special situation.
when programming, from Hibernate, I also use JDBC to change this level? does hibernate provide directly interface to let me change isolation level(not throught JDBC connection)?
I have another question, because I'm using hibernate in ejb container, and set container managed transaction, so configure hibernate to use JTA.
and when configured to JTA, I even don't need to commit, this was finished by container, and the transaction kind was configured in ejb's xml file , including (require, require new ,never etc). shall I set the isolation level throught JDBC? or can I configure this to Bean's xml file?
do you mean


Top
 Profile  
 
 Post subject: Setting isolation level directly
PostPosted: Tue Aug 22, 2006 9:07 am 
Newbie

Joined: Wed Aug 16, 2006 6:29 pm
Posts: 9
It is possible to get the underlying JDBC connection from a Hibernate session and to change its isolation level like this:

session.connection().setTransactionIsolation(4);

However it might be difficult to ensure the isolation level is reset back on the JDBC connection in the scenario you're describing.

Consider re-thinking what you're trying to achieve. If it's a very limited use case maybe you can accomplish it by using Hibernate directly (and not through the EJB container) in that one case. The advantage is it is easier to reset the JDBC connection before it's returned to the pool.

I would also consider using locks to make up for defficiencies in isolation. A number of Hibernate Session and Query methods allow you to specify a LockMode with different locking levels for the data you're retrieving.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 22, 2006 9:01 pm 
Newbie

Joined: Mon Aug 21, 2006 9:15 pm
Posts: 17
rstoya05-aop, thank you very much for you detailed reply.
In fact, I havn't begun my designing. and these days I'm learning ejb container and hibernate. I was assigned a task to use ejb container and hibernate to design a c/s application.
And I was so puzzled for the behavior of the EJB container's JTA(both CMT and BMT). CMT and BMT both provide transaction management, but internally, they both use JTA to operate transaction(not using JDBC connection). so I don't know whether it comply with the transaction isolation level setting(through Connection). And when using hibernate in ejb container, it is recommended to configure JTA as hibernate's Transaction management. And I look up JTA specification, it even doesn't mentioned isolation level, I was confused, why JTA doesn't include isolation level API?
As you have said, I think I can resolve isolation problem by some other manner(like row lock), but in some situation it is so easy for use oracle's read only isolation level(because it provide a snapshot for all table's all data, so the data in the transaction will be consistent and will not be affected by any other transaction), and I think it will be so difficult to obtain this function if I don't use read only isolation level.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 22, 2006 9:05 pm 
Newbie

Joined: Mon Aug 21, 2006 9:15 pm
Posts: 17
rstoya05-aop, thank you very much for you detailed reply.
In fact, I havn't begun my designing. and these days I'm learning ejb container and hibernate. I was assigned a task to use ejb container and hibernate to design a c/s application.
And I was so puzzled for the behavior of the EJB container's JTA(both CMT and BMT). CMT and BMT both provide transaction management, but internally, they both use JTA to operate transaction(not using JDBC connection). so I don't know whether it comply with the transaction isolation level setting(through Connection). And when using hibernate in ejb container, it is recommended to configure JTA as hibernate's Transaction management. And I look up JTA specification, it even doesn't mentioned isolation level, I was confused, why JTA doesn't include isolation level API?
As you have said, I think I can resolve isolation problem by some other manner(like row lock), but in some situation it is so easy for use oracle's read only isolation level(because it provide a snapshot for all table's all data, so the data in the transaction will be consistent and will not be affected by any other transaction), and I think it will be so difficult to obtain this function if I don't use read only isolation level.


Top
 Profile  
 
 Post subject: Use object identity scope and versioning for repeatable read
PostPosted: Tue Aug 22, 2006 10:49 pm 
Newbie

Joined: Wed Aug 16, 2006 6:29 pm
Posts: 9
One thing to keep in mind, which might help is that thanks to Hibrenate's session, which is transaction-scoped, Hibernate automatically provides repeatable reads for retreived objects regardless of database settings.

This is so because objects retrieved from the database are cached in Hibernate's session for the duration of the transaction. Any new queries are guaranteed to bring back the same objects as those already in the cache.

You should also look into Hibernate's versioning feature (optimistic locking) as well the API for pessimistic locking of rows, which I already mentioned.

Check here:
http://www.hibernate.org/hib_docs/v3/re ... tions.html


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 23, 2006 9:01 am 
Newbie

Joined: Mon Aug 21, 2006 9:15 pm
Posts: 17
yes, when transaction level was set to repeatable ,I think Hibernate can resolve this problem because of session's cache. but I'm afraid that it's uncertainty.

1: when sometime I use Connection through Hibernate Session's getConnection() method, and execute direct SQL statment, then results will not be loaded into session's cache.

2: if I use session.load to load one Persistent Object by object id(loaded into cache, for example:object id is 1 ), then when I call session.load through that object id(with value 1) the second time, it will load data from cache(not from database). but if I call session.createQuery(from Object where ****), I think hibernate will convert this to sql statement, and execute to database, whether hibernate keep the old value of id 1 or update the new value? this situation I will do some test today, I think Hibernate will keep old value. so just as you said, Hibernate provide repeatable read.

I'd like to use serializable level, and serializable level provide not only repeatable read, also it can avoid illusive read. but Hibernate session's cache can't resolve this problem.

And from my opinion, I admit we can resolve most question through hibernate's function. but this isn't the reason for us not using transaction level, I can resolve one problem throught hibernate, and also I can resolve it by set transaction level. but the problem is: container's BMT and CMT seems don't provide any function to set transaction level. why?
And when using hibernate's direct Connection(or DataSource's Connection), we can set transaction level, but can it work? container use JTA to control transaction, and in our code, we use database Connection's transaction level, I don't know result. but I will do some test for this.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 23, 2006 11:52 am 
Newbie

Joined: Wed Aug 16, 2006 6:29 pm
Posts: 9
Quote:
container's BMT and CMT seems don't provide any function to set transaction level. why?


JTA manages global transactions like two-phase commits. It tells Hibernate when to begin a transaction and when to commit. But it's Hibernate that actually does the work of starting and ending transactions.

I believe the transaction isolation can be set through Hibernate even when using JTA. However, what you want to do is change it on the fly and that may be harder to do if using declarative transaction management because it's JTA that's managing the transactions.

Have you tried finding a JTA specific forum and posting a question there?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 23, 2006 9:08 pm 
Newbie

Joined: Mon Aug 21, 2006 9:15 pm
Posts: 17
rstoya05-aop wrote:
Quote:
container's BMT and CMT seems don't provide any function to set transaction level. why?


JTA manages global transactions like two-phase commits. It tells Hibernate when to begin a transaction and when to commit. But it's Hibernate that actually does the work of starting and ending transactions.

I believe the transaction isolation can be set through Hibernate even when using JTA. However, what you want to do is change it on the fly and that may be harder to do if using declarative transaction management because it's JTA that's managing the transactions.

Have you tried finding a JTA specific forum and posting a question there?


now I get understanded JTA and hibernate(or DataSource)'s relationship. you are right, and when using CMT, I can't control transaction level be set back, because I can't control traction's begin and end. thank you very much, this problem puzzled me long time.

So as the result , CMT and transaction level can't worked together. I ever post this problem to theserverside.com, but no any answer. thank you.


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.