aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate Transaction Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate Transaction" Watch "Hibernate Transaction" New topic
Author

Hibernate Transaction

Chirag Sharma
Greenhorn

Joined: Jun 27, 2012
Posts: 24
Hi Friends,

I am working with Hibernate. I want to know that is transaction block thread safe?
For example:

transaction = session.beginTransaction();
// some code here
selectMaxIdFromDB();
// some code here
transaction .commit();


I want to know that is it better approach to generate a String unique Id here? I don't want to use
Synchronized block to get unique id in multithread environment.


thanks in advance
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3454
    
  47

No, that block of code is not thread-safe. The exact behavior depends on the transaction isolation level and the database's concurrency model, among other things, but you may easily end up getting two identical IDs.

Hibernate provides its own tools for generating unique identifiers, you can even plug a custom generator in. I don't use Hibernate myself, so I cannot provide a direct answer, but searching for hibernate unique id generation brings up some interesting topics you can comb through.

Or perhaps someone else will be able to tell you. I've moved your question to a forum where Hibernate folks usually hang out.
Chirag Sharma
Greenhorn

Joined: Jun 27, 2012
Posts: 24
Thanks Martin for reply....

it's helpfull. The hibernate unique id generation is automated. I am looking for an id that should be like "REQ"+pkID
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1632
    
    7

Have a look at this blog, it gives a good overview. Definitely pick one of the provided strategies over generating your own.

http://blog.eyallupu.com/2011/01/hibernatejpa-identity-generators.html


[How To Ask Questions][Read before you PM me]
Chirag Sharma
Greenhorn

Joined: Jun 27, 2012
Posts: 24
Thanks Bill for reply....

I have checked that link. He is using Sequence with annotation.

I am using MySql database. and using hbm.xml files i hibernate 3.0

Ok ... I am going to tell..for wat i am looking for:
.....................................................................................................
we are saving a record using <generator> increment </generator>
.... we want to save another column of this record like "REQ"+[max(pkId)+1]
...........................

It creates problem in multithreaded environment.
Don't wanna use Synchrinized as lot of users are there.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3454
    
  47

Chirag Sharma wrote:
we are saving a record using <generator> increment </generator>
.... we want to save another column of this record like "REQ"+[max(pkId)+1]

Regardless of whether you're using Hibernate or not, this cannot be done without synchronization at some point, either in your application (assuming that only your application modifies the data), or in the database. Even database tools providing this functionality (identity columns or sequences, depending on the DB) are synchronized at some point, but they are optimized for access of speed in a way that usually affect the properties of the generated numbers (most often the sequence is not guaranteed to be gapless, and sometimes the numbers are not assigned in strictly increasing order).

If you need to perform transactions quickly in multiuser environment, your requirement is a big hindrance and is therefore avoided as much as possible. Why do you need to assign these numbers in the way you've described?
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1632
    
    7

Yes for your situation increment strategy wont work. From the blog I linked to:

Clustering – Some generators (as the Hibernate specific ‘increment’ generator) cannot be safely used in a clustered environment


This is for the same reason you are discussing now.


I would look at using identity, it sounds like that is what you are looking for. You need to specify on the schema to auto increment the column.



Note you will get better performance out of the Table strategy though.
Chirag Sharma
Greenhorn

Joined: Jun 27, 2012
Posts: 24
Hi Bill,

can i auto Increment the string id?
Chirag Sharma
Greenhorn

Joined: Jun 27, 2012
Posts: 24
Why do you need to assign these numbers in the way you've described?


Hi martin;

actually this String Id is used when we view the record. We show String Id in place of Pk Id.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3454
    
  47

Chirag Sharma wrote:actually this String Id is used when we view the record. We show String Id in place of Pk Id.

In this case, use the primary key and format it for display whatever you like. You could just glue the "REQ" prefix to the actual PK, for example.

If this is inadequate, there are probably other requirements you haven't described yet.
Bhoopendra Singh
Greenhorn

Joined: Feb 21, 2012
Posts: 3
Since this is a derived value, why do you want store?
In case you want to do without any synchronization , you can do in two steps. First insert records and than update column with derived value. I am assuming pkid is id for new records and you don't need to get this value as max(..).
Chirag Sharma
Greenhorn

Joined: Jun 27, 2012
Posts: 24
HI Bhooopendra,

I am using your last suggestion.

Thanks to all to reply.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate Transaction
 
Similar Threads
Designing in Hibernate
Exception handling
try, catch,finally
Problem about :Config the EventListener (org.hibernate.event) in ApplicationContext.xml
Help with preventing duplicate elements in foreign key column