-->
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: HibernateUtil and JNDI
PostPosted: Mon Jul 11, 2005 4:49 am 
Newbie

Joined: Mon Jul 11, 2005 4:35 am
Posts: 7
Hi

I am using JNDI to access the SessionFactory. My question is, do I
still need to put the creation of the SessionFactory inside a
HibernateUtil class like on page 297, or is it enough to the use the JNDI?

My code looks like this without using HibernateUtil. In this way, is the
Sessionfactory initiliazed just once without using HibernateUtil?
Accordingly, do I in my case need to use a HibernateUtil class?

// Create session
SessionFactory sessionFactory = null;
try {
Context ctx = new InitialContext();
Object obj = ctx.lookup("java:/hibernate/HibernateFactory");
sessionFactory = (SessionFactory) obj;
} catch (NamingException e) {
throw new DataAccessException("Error looking up HibernateFactory
through JNDI");
}
Session session = sessionFactory.openSession();
// Perform transaction
Transaction customerTransaction = null;
try {
customerTransaction = session.beginTransaction();
session.save(customer);
customerTransaction.commit();
} catch (HibernateException e) {
customerTransaction.rollback();
throw new DBTransactionException("Transaction error. Transaction
rolled back");
} finally {
session.flush();
session.close();
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 11, 2005 6:17 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
No, you don't need HibernateUtil at all if this is working for you. I don't know how you get the SessionFactory into JNDI, and without the static{} block in HibernateUtil I can imagine you are probably using JBoss and JMX deployment to do this. HibernateUtil just makes things easily portable.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 11, 2005 8:42 am 
Newbie

Joined: Mon Jul 11, 2005 4:35 am
Posts: 7
Below is an extract from my hibernate.cfg.xml. My app server is
Oracle Appliction Server Containers for J2EE.
The connection.datasource is configured through the app server.

On page 126 in the book it's said that it's extremely expensive
to create a SessionFactory. So putting it into JNDI like I've
done prevents from creating a new one each time I look it up?

In the Hibernate Reference Documentation chapter 4.8.3 it stands that you
can use getCurrentSession (instead of openSession my remark) in
SessionFactory to obtain a session. But I'm not successful in this. Is there
any more configuration needed or which of the two methods should I use?


<session-factory name="java:/hibernate/HibernateFactory">

<property name="connection.datasource">java:comp/env/jdbc/OracleDS</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
<property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.OrionTransactionManagerLookup</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>

</session-factory>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 11, 2005 8:46 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
The question is how you put it into JNDI. This doesn't happen by magic, but by calling buildSessionFactory() on a Configuration object (thats what the static block in HibernateUtil does) or by using some kind of container-managed deployment, like a JMX service.

For your other questions please use the regular Hibernate user forum. I also recommend you study the reference documentation's "Configuration" chapter again. Remember that Hibernate in Action is about Hibernate 2.x and you are trying to use H3 features.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 11, 2005 9:41 am 
Newbie

Joined: Mon Jul 11, 2005 4:35 am
Posts: 7
So if I write a HibernateUtil class like this (see pages 297 and 314)

public class HibernateUtil {

static {
try {
new Configuration().configure().buildSessionFactory();
}
catch (Throwable e) {
e.printStackTrace();
}
}

public static SessionFactory getSessionFactory() {
SessionFactory session = null;
try {
Context ctx = new InitialContext();
String jndiName = "java:hibernate/HibernateFactory";
sessions = (SessionFactory)ctx.lookup(jndiName);
}
catch (NamingException e) {
e.printStackTrace();
}
return sessions;
}

public static Session getSession() throws HibernateException {
return getSessionFactory.openSession();
}
}

and use this class to get a session like this

Session session = HibernateUtil.getSession();

then I've been preventing creating a new SessionFactory for each
request?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 11, 2005 9:53 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Why don't you try to understand the code, line by line, before putting it in production.


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.