-->
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: Should I call "session.beginTransaction()" when I
PostPosted: Fri Oct 22, 2004 2:16 am 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
Hi everyone:

I know if I save some data to database , I should use transaction like this:

Code:
Session session=HibernateUtil.currentSession();
Transaction t=session.beginTransaction();

User u=new User();
.....................
...............
session.save(u);
t.commit();
HibernateUtil.closeSession();


It means that I need have to use a transaction.

If I query database only, Should I use transaction?
For example:

Code:

Session session=HibernateUtil.currentSession();
Transaction t=session.beginTransaction();  // <----It is nessecery to
                                                              //begin
                                                           //transaction here?
List list=session.find("from User");
.....................
Broker b=(Broker)session.get(Broker.class,broker_id);
.....................

t.commit();
HibernateUtil.closeSession();


I think it should be changed to following:
Code:
Session session=HibernateUtil.currentSession();

List list=session.find("from User");
.....................
Broker b=(Broker)session.get(Broker.class,broker_id);
.....................

HibernateUtil.closeSession();


The later will be better I think. Is it correct? Thks!

_________________
You are not alone...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 22, 2004 2:42 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
always use transaction no matter if you are reading, updating, inserting

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 22, 2004 2:51 am 
Expert
Expert

Joined: Tue Oct 05, 2004 9:45 am
Posts: 263
@anthony
it might be a really silly question (maybe answered in the booked - it's ordered but has 14 days to delivery) ...
but why should i open a transaction when i really only want to read information? is there a short explanation for that?
Sorry for going hibernate-OT ... i thing that's something that should be done even if i'm not using hibernate ...

I believe yo, and will change that in my codings, but i would like to understand why ;)

thx!
curio


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 22, 2004 2:53 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Short explanation: some database management systems need transaction demarcation for read operations, since they manage locks even for reading. Others don't, but you'd like to keep your code portable and clean (and frankly, there is no performance penaltity or other disadvantage, and your code is much easier to understand). So, you could ask this question as "Why do you think you _don't_ need it?

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 22, 2004 2:55 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
oh, the best way is to ask google... or search the forum, this has been discussed many times

but you're right... you have to trust me ;)

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 22, 2004 3:03 am 
Expert
Expert

Joined: Tue Oct 05, 2004 9:45 am
Posts: 263
@christian
That really sounds good to me ... and knowing that different databases do reading different makes it more clear ...
so i will change my coding ...

@anthony
sure, i trust you (in hibernate and db-questions :) ;) ) ... my knowledge about database-systems and or-layers are more than only far away from yours (and the rest of the hibernate team) .... and that's good ... don't want to think about what hibernate would be with my poor-db-knowledge ;) ... but i'm working on not asking silly questions in the forum ;) and filling my gaps ....

thx for explanation :)

gtx
curio


Top
 Profile  
 
 Post subject: :(
PostPosted: Fri Oct 22, 2004 5:35 am 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
anthony wrote:
always use transaction no matter if you are reading, updating, inserting


Thks for your reply : )

But I found a problem: When I query database only,if I use transaction ,Tomcat will report warn:

Code:

15:56:48,781  WARN SessionImpl:3390 - afterTransactionCompletion() was never cal
led

I don't know why this happen. : (

Should I use
Code:
  Transaction t=s.beginTransaction();
?

_________________
You are not alone...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 22, 2004 6:45 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
read again some code example in the reference guide, looks like

1- get session
2- tx = session.beginTransaction
3- do job
4- tx.commit
5- session.close

in catch block: tx.rollback + session.close

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 22, 2004 6:49 am 
Expert
Expert

Joined: Tue Oct 05, 2004 9:45 am
Posts: 263
i think one place where the 'afterTransactionCompletion'-method gets called is during a 'commit()'.
Silly question perhaps .. but do you 'commit' after reading?

gtx
curio


Top
 Profile  
 
 Post subject: : (
PostPosted: Fri Oct 22, 2004 1:19 pm 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
anthony wrote:
read again some code example in the reference guide, looks like

1- get session
2- tx = session.beginTransaction
3- do job
4- tx.commit
5- session.close

in catch block: tx.rollback + session.close


But the HibernateUtil's closeSession method will close current session.
This class is in the hibernate docs.

Code:

public class HibernateUtil {

    private static final SessionFactory sessionFactory;

    static {
        try {
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (HibernateException ex) {
            throw new RuntimeException("Exception building SessionFactory: " + ex.getMessage(), ex);
        }
    }

    public static final ThreadLocal session = new ThreadLocal();

    public static Session currentSession() throws HibernateException {
        Session s = (Session) session.get();
        // Open a new Session, if this Thread has none yet
        if (s == null) {
            s = sessionFactory.openSession();
            session.set(s);
        }
        return s;
    }

    public static void closeSession() throws HibernateException {
        Session s = (Session) session.get();
        session.set(null);
        if (s != null)
            s.close();
    }
}


Why session is not close some time after I invoke the HibernateUtil.closeSession() method? It is memory leak?

_________________
You are not alone...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 22, 2004 1:49 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
if you use hibernateUtil, forget session.close

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject: :)
PostPosted: Sat Oct 23, 2004 8:57 am 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
anthony wrote:
if you use hibernateUtil, forget session.close


I think I should change the code to this:

Code:

try{
Session session=HibernateUtil.currentSession();
Transaction t=session.beginTransaction();  // <----It is nessecery to
                                                              //begin
                                                           //transaction here?
List list=session.find("from User");
.....................
Broker b=(Broker)session.get(Broker.class,broker_id);
.....................

}catch(...){
.............
}finally{
t.commit();
HibernateUtil.closeSession();
}


This maybe avoid the problem.

_________________
You are not alone...


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 23, 2004 10:33 am 
Senior
Senior

Joined: Sun Jan 04, 2004 2:46 pm
Posts: 147
In short always use a transaction, there's negligible overhead and it can avoid problems that are hard to pin down later. Generally do tx.commit() at the end of your try block, tx.rollback() in your catch block and session.close() in your finally block.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 23, 2004 12:20 pm 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
Myk wrote:
In short always use a transaction, there's negligible overhead and it can avoid problems that are hard to pin down later. Generally do tx.commit() at the end of your try block, tx.rollback() in your catch block and session.close() in your finally block.


Thank you Myk!

The final code is this :
Code:

Session session=null;
Transaction t=null;
try{
    session=HibernateUtil.currentSession();
    t=session.beginTransaction(); 
                                                                                                           
List list=session.find("from User");
.....................
Broker b=(Broker)session.get(Broker.class,broker_id);
.....................
t.commit();
}catch(...){
session.rollback();
.............
}finally{
try{
HibernateUtil.closeSession();
}catch(..){
............
}

}


It will work no problem. : )

_________________
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.