-->
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: Lazy Load
PostPosted: Wed Feb 25, 2004 11:09 am 
Beginner
Beginner

Joined: Wed Feb 25, 2004 10:58 am
Posts: 43
I am new to hibernate , could someone please let me know if there is any problem with doing the following to lazy intialize a collection in a web app .

I hava a DAO
In the app I get the user
UserDAO.getUser(pk);

Later in the app I load the roles only when I need them
UserDAO.loadRoles(user);


The load roles looks like this


public void loadRoles(User user) {

Session session = null;
Transaction tx = null;

try {

session = HibernateUtil.currentSession();
tx = session.beginTransaction();
session.lock(user, LockMode.NONE);
Hibernate.initialize(user.getRoles());
tx.commit();

} catch (HibernateException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
HibernateUtil.closeSession();
} catch (HibernateException e) {
e.printStackTrace(); }
}

}



Seems to work fine , my question is is this bad practice ? I have already looked into the docs for long running transactions and keeping the hibernate session in http session and would rather pursue a option where I don't have to persist the hibernate session . Also , when I take out the session.lock .. it does not work .


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 25, 2004 11:19 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
tx = session.beginTransaction();
tx.commit();
you don't need it since you're not updating any objects

session.lock(user, LockMode.NONE);
Hibernate.initialize(user.getRoles());
can be replaced by
session.refresh(user); (if someone has updated the object, you'll see the changes)
user.getRoles();


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 25, 2004 11:48 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
delpouve wrote:
tx = session.beginTransaction();
tx.commit();
you don't need it since you're not updating any objects

Why on earth reading does not need transaction ?
Please refer to the famous Gavins Grrrrmmrg posts on that subject.

This is for sure something to add into the book.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 25, 2004 12:02 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
cannot find the grrrr post,
what does tx.commit() do if the only things you do is reading objects?

i must lack knowledge about this, excuse me


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 25, 2004 12:17 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
To keep read consistency, you must be in Tx.
If you don't your driver et db will be in autocommit=true (not sure of me) and provoque overload due to tx creation.
Anybody better than me correct it if I'm wrong.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 25, 2004 12:39 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
when you use the session, it's based on a jdbc connection, so automatically a transaction is started ...
Now if you write tx.commit() or let auto-commit do his work, is it different?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 25, 2004 12:50 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Yes because auto commit will be done each time a statement is flushed into the DB, if you explicitly do a commit() then you can avoid extra commit per flush.
And my first remark stray true.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 25, 2004 12:51 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
ok fine, i understand,

merci Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 25, 2004 2:58 pm 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
delpouve wrote:
cannot find the grrrr post,
what does tx.commit() do if the only things you do is reading objects?

i must lack knowledge about this, excuse me


"tx.commit()" ends transaction and releases read locks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 25, 2004 3:34 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
i think it could be usefull to explain that in the doc


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.