-->
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.  [ 3 posts ] 
Author Message
 Post subject: long lasting transactions
PostPosted: Mon Mar 15, 2004 2:03 pm 
Beginner
Beginner

Joined: Mon Feb 09, 2004 6:49 am
Posts: 21
Hi

I'm using Hibernate version 2.1.2, MySQL version 4.0.18

I have a question regarding long transactions using session.disconnect() and session.reconnect(). Is it dangerous to simply make a method that returns the session in a disconnected state, and whenever I make a connection to the DB I reconnect, do what I want, and then diconnect like this:

//+ imports;

public class UserService
{

public User getUser(String userName)throws HibernateException
{
User user = null;
Session session = HibernateUtilities.currentSession();
session.reconnect();
String hql="from people.Person as user where" + "userName='"+userName+"'";
List list = session.find(hql);
for(Iterator iter = list.iterator();iter.hasNext();)
{
user = (User)iter.next();
}
session.disconnect();
return user;
}end getUser

public void saveOrUpdateToDatabase(Object object) throws HibernateException
{
Session session;
Transaction transaction = null;
try {
session = HibernateUtilities.currentSession();
session.reconnect();
transaction = session.beginTransaction();
session.saveOrUpdate(object);
transaction.commit();
session.disconnect();
} catch (HibernateException e) {

if(transaction!=null)
transaction.rollback();
e.printStackTrace();
}
} // end saveToDatabase

}

//+imports

public class HibernateUtilities {

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 local = new ThreadLocal();

//Returns a disconnected Session object
public static Session currentSession() throws HibernateException {
Session session = (Session) local.get();

if (session == null) {
session = sessionFactory.openSession();
session.disconnect();
local.set(session);
}
return session;
}

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

I experience no problem doing it like this, but i wonder if it is not the most efficent way around.. Also I'm not sure if it is ok to load an object from a database lik I do in getUser(String userName), work with it an its lazy collections, and then update it in DB using the method saveOrUpdateToDatabase(Object object)..


--dag--


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 15, 2004 2:18 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
String hql="from people.Person as user where userName= :userName'";
Query q = session.createQuery(hql);
q.setProperties("transferBean); --> look at query API
List list = query.list();
user = (User)list.get(0);

Only use iterate() method when you need because it execute one sql query per result to have the detail.
List() method execute only one optimised sql query.


If userName is the id then use session.load(....)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 15, 2004 2:20 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
oh, i forgot, you should consider using transaction even for read-only code


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