-->
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.  [ 5 posts ] 
Author Message
 Post subject: Log file of the Hibernate getting huge.. is this how it is ?
PostPosted: Fri Jun 02, 2006 2:37 am 
Newbie

Joined: Tue May 02, 2006 1:59 pm
Posts: 17
Location: California
Hello,
I am using Hibernate 3.1.
My hibernate.cfg.xml is as follows :

<hibernate-configuration>
<session-factory name="foo">
<property name="hibernate.cglib.use_reflection_optimizer">true</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@x.x.x.x:xxxxxxx</property>
<property name="hibernate.connection.username">xx</property>
<property name="hibernate.default_schema">xxxxxxxx</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="VsDomainWorkflowRef.hbm.xml" />
<mapping resource="VsRequestFieldT.hbm.xml" />
<mapping resource="VsSystemConfigurationT.hbm.xml" />
<mapping resource="VsBrandCountryMapT.hbm.xml" />
<mapping resource="VsDbmsResourcesRef.hbm.xml" />
<mapping resource="VsMdnsAccountT.hbm.xml" />
<mapping resource="X.hbm.xml" />
<mapping resource="VsDbmsRegLevelRef.hbm.xml" />
...... 50 more maping files

I have a layered architecture,

For each hibernate operation, a session is opened using a threadLocal logic.

Example:
public VsDnsProfile findByPrimaryKey(Long id) {
Session session = null;
VsDnsProfile record = null;
try {
session = HibernateUtil.currentSession();
record = new VsDnsProfile();
record = (VsDnsProfile) session.get(VsDnsProfile.class, id);

} catch (Exception e) {

e.printStackTrace();
} finally {
if (session != null) {
try {
HibernateUtil.closeSession();

} catch (Exception e) {
e.printStackTrace();
}
}
}

return record;
}


-------------

HibernateUtil:


public class HibernateUtil {

private static final SessionFactory sessionFactory;

static {
// Create the SessionFactory
sessionFactory = new Configuration().configure().buildSessionFactory();
}

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();
}
}


The log has many select statemenst which were not performed by my application : Is there some setting I need to set, is the performance getting bad with so many accesses to database.

Is the stragtegy of openeing a session and closing a session for each hibernate transcation correct ?


Thanks
Shobhana

_________________
Thanks


Top
 Profile  
 
 Post subject: Session usage
PostPosted: Fri Jun 02, 2006 2:52 am 
Newbie

Joined: Fri Jun 02, 2006 2:42 am
Posts: 2
I see nothing wrong with the way you handle session but i use an Assembler(J2EEpattern) to gather all calls(database/hibernate) into one Session.

A more common technique to do this is to begin the transation in a servlet filter and commit/rollback transaction and close session it at the end of the filter.

I dont think you should open/close commit/begin at every call, but maybe i misundestood your question.

see http://www.hibernate.org/43.html
this explains a lot of what im trying to say.
My difference to this example is that i do a session.close() in a finally clause that this example does not (dont know why though).

/robert


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 02, 2006 1:04 pm 
Regular
Regular

Joined: Mon May 22, 2006 2:30 pm
Posts: 74
What exactly do you mean when you say you have a session for each "Hibernate operation"? A natural usage pattern is to have a session per "business transaction". Creating and closing a session for something as low-level as a "findByPrimaryKey" operation would not fit that pattern at all. It would result in a lot of unecessary session overhead. But your statement about too many database accesses doesn't necessarily correlate with your session usage. The session would help only in those cases where the session cache would contain persisitent objects that are used throughout your business transaction. If you keep closing and opening sessions, you won't be able to use the cache, and they will have to be retrieved from the database each time.


Last edited by hibernate_user on Fri Jun 02, 2006 1:10 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 02, 2006 1:09 pm 
Newbie

Joined: Tue May 02, 2006 1:59 pm
Posts: 17
Location: California
Thanks for the reply.

What I meant is that I have DAO layer, which has all the data access operations like(save,load, findByprimaryKey, etc ..)

For each of this operations I do the follwoing things :

Example to load a object with a primary key
------------------------------------------------------------------>
public VsDnsProfile findByPrimaryKey(Long id) {
Session session = null;
VsDnsProfile record = null;
try {
session = HibernateUtil.currentSession();
record = new VsDnsProfile();
record = (VsDnsProfile) session.get(VsDnsProfile.class, id);
} catch (Exception e) {
LogDbmsInfo("Exception VsDnsProfileDAOImpl");
e.printStackTrace();
} finally {
if (session != null) {
try {
HibernateUtil.closeSession();

} catch (Exception e) {
e.printStackTrace();
}
}
}

return record;
}

------------------------------------------------------------->



Thanks
Shobhana

_________________
Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 02, 2006 1:12 pm 
Regular
Regular

Joined: Mon May 22, 2006 2:30 pm
Posts: 74
Your session logic should definitely be in a layer above the DAO layer.


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