-->
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.  [ 6 posts ] 
Author Message
 Post subject: Hibernate in J2EE: issues on Session and Transactions
PostPosted: Thu Apr 29, 2004 4:38 am 
Beginner
Beginner

Joined: Thu Apr 29, 2004 4:31 am
Posts: 42
hi all,
i m a beginner with Hibernate and i want to ask few questions about how can i use it in a J2EE app.
I have implemented a 'PersistenceManager' layer in my app, which is in charge of interacting with the database.
My app is a struts-based app, so i have downloaded the hibernate plugin for struts which instantiates a SessionFactory.
question 1: do i have to open/close/refresh a transaction every time my persistence manager has to do some operation (insert/delete/update)?
question 2: do i have to start and commit a transaction every time i want to do an insert/update/delete?

thanx in advance and regards
marco


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 29, 2004 6:19 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
hi,
i've tried struts plug in and threadlocal session, i prefer threadlocal.... you should take a look.

Your questions are related to transaction management.
Hibernate does not have "specific" transaction management so just use it as if you were working in jdbc.
I think the most important is to understand "flush", for this reaf the reference doc, it is well explained
And last advices:
1- consider hibernate session as a business process cache
2- hibernate session is not a "global" cache (second level cache)
3- Hibernate session is not threadsafe, using threadlocal session help you to prevent big errors
4- in a web app, the best practice is one hibernate session per hhtp request, in you need other way, you can do it but you must know what you are doing


Anthony


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 29, 2004 8:22 am 
Beginner
Beginner

Joined: Thu Apr 29, 2004 4:31 am
Posts: 42
Hi Anthony,
thanx for the advice.... i have one more question on transactions..
reason of why i have asked is that in some examples, when creating an object, the code is 'wrapped' in a transaction, while in other cases there is no transaction at all...
so, from your reply i assume following code is perfectly valid (meaning, there is no transaction code at all..)

ThreadLocal session = new ThreadLocal90

SessionFactory sf = (SessionFactory) new InitialContext().lookup("SessionFactory");

Session s = sf.openSession();
session.set(s);

Test myclass = new Test();
mclass.setDescription("foobar");

session.save(myclass);


regards
marc


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 29, 2004 8:37 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
with your code, your changes will simply not be persisted

this code
Code:
ThreadLocal session = new ThreadLocal90

SessionFactory sf = (SessionFactory) new InitialContext().lookup("SessionFactory");

Session s = sf.openSession();
session.set(s);


does not need transaction because it's only an "initialisation"

now, the second part of code should be
Code:
Session session = getSession(); //that is some custom DAO method to retrieve threadLocal session --> session.get() + reconnect if necessary
Transaction tx = session.beginTransaction();

Test myclass = new Test();
mclass.setDescription("foobar");
session.save(myclass);

tx.commit();
session.close(); // this can be done in the filter depending how you want to control connexion pooling



Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 29, 2004 8:46 am 
Beginner
Beginner

Joined: Thu Apr 29, 2004 4:31 am
Posts: 42
Hi anthony,
ok i got the answer myself by reading docs... whenever i create an object i have to do it within a transaction..

thanx 4 ur help anyway and regards
marco


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 29, 2004 8:50 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
even for a simple select you must use a transaction = best practice


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