-->
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.  [ 12 posts ] 
Author Message
 Post subject: No TransactionManagerLookup specified error
PostPosted: Fri Oct 07, 2005 2:03 pm 
Beginner
Beginner

Joined: Sun Dec 05, 2004 10:25 am
Posts: 23
I am trying to use the dao pattern used in the CaveatEmptor 3.1 updated code. I pretty much at this point have most of it as it out of the box but when I am try to get a DAO that I made I am getting a org.hibernate.HibernateException: No TransactionManagerLookup specified error. Can someone please help me to see what I am missing. I debugged the code and did notice that the sessionFactory that is being returned does have a null value for TransactionManager. I am still kind of a Hibernate rookie. So if this is something stupid that I am overlooking please forgive me and explain. Thank you

Hibernate version:
3.0

Code between sessionFactory.openSession() and session.close():
public class HibernateDAOFactory extends DAOFactory{

/* (non-Javadoc)
* @see edu.bju.aem.util.dao.DAOFactory#getServiceDAO()
*/
@Override
public ServiceDAO getServiceDAO() {
return new ServiceDAOHibernate(getCurrentSession());
}

protected Session getCurrentSession() {
// Get a Session and begin a database transaction. If the current
// thread/EJB already has an open Session and an ongoing Transaction,
// this is a no-op and only returns a reference to the current Session.
// The advantage of starting a transaction here is that it is lazy, i.e.
// no transaction is started until it is really needed (when a DAO is
// constructed).
HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
return HibernateUtil.getSessionFactory().getCurrentSession();
}

using the hibernate util class from CaveatEmptor 3.1

static {
// Create the initial SessionFactory from the default configuration files
try {

// Replace with Configuration() if you don't use annotations or JDK 5.0
// configuration = new AnnotationConfiguration();
configuration = new Configuration();

// This custom entity resolver supports entity placeholders in XML mapping files
// and tries to resolve them on the classpath as a resource
configuration.setEntityResolver(new ImportFromClasspathEntityResolver());

// Read not only hibernate.properties, but also hibernate.cfg.xml
configuration.configure();

// Assign a global, user-defined interceptor with no-arg constructor
String interceptorName = configuration.getProperty(INTERCEPTOR_CLASS);
if (interceptorName != null) {
Class interceptorClass =
HibernateUtil.class.getClassLoader().loadClass(interceptorName);
Interceptor interceptor = (Interceptor)interceptorClass.newInstance();
configuration.setInterceptor(interceptor);
}

if (configuration.getProperty(Environment.SESSION_FACTORY_NAME) != null) {
// Let Hibernate bind the factory to JNDI
configuration.buildSessionFactory();
} else {
// or use static variable handling
sessionFactory = configuration.buildSessionFactory();
}

} catch (Throwable ex) {
// We have to catch Throwable, otherwise we will miss
// NoClassDefFoundError and other subclasses of Error
log.error("Building SessionFactory failed.", ex);
throw new ExceptionInInitializerError(ex);
}
// try {
//// Create the SessionFactory
// sessionFactory = new Configuration().configure().buildSessionFactory();
// } catch (Throwable ex) {
//// Make sure you log the exception, as it might be swallowed
// log.error("Initial SessionFactory creation failed.", ex);
// throw new ExceptionInInitializerError(ex);
// }
}

public static SessionFactory getSessionFactory() {
SessionFactory sf = null;
String sfName = configuration.getProperty(Environment.SESSION_FACTORY_NAME);
if ( sfName != null) {
log.debug("Looking up SessionFactory in JNDI.");
try {
sf = (SessionFactory) new InitialContext().lookup(sfName);
} catch (NamingException ex) {
throw new RuntimeException(ex);
}
} else {
sf = sessionFactory;
}
if (sf == null)
throw new IllegalStateException("SessionFactory not available.");
return sf;
}

Full stack trace of any exception that occurs:
org.hibernate.HibernateException: No TransactionManagerLookup specified
at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:503)
at edu.bju.aem.util.persistance.dao.hibernate.HibernateDAOFactory.getCurrentSession(HibernateDAOFactory.java:26)
at edu.bju.aem.util.persistance.dao.hibernate.HibernateDAOFactory.getServiceDAO(HibernateDAOFactory.java:16)
Name and version of the database you are using:
Postgres 8.0


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 07, 2005 2:10 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
In Hibernate 3.0 and current 3.1 releases, this line only works if you run in an application server:

HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();

In CVS and in the upcoming 3.1rc1 release, this will work everywhere. For the old stuff you need a more powerful HibernateUtil, http://hibernate.org/42.html for example. The new release will have this built-in with the SessionFactory.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 07, 2005 2:30 pm 
Beginner
Beginner

Joined: Sun Dec 05, 2004 10:25 am
Posts: 23
ok so in the HibernateDAOFactory getSession() should I just return the session like
return HibernateUtil.getCurrentSession and then in my code use HibernateUtil.beginTransaction and commitTransaction? Not sure where to start and stop the transaction.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 07, 2005 4:07 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Whenever you think your unit of work beings and when it should end. Really, this can go anywhere.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 10, 2005 9:30 am 
Beginner
Beginner

Joined: Sun Dec 05, 2004 10:25 am
Posts: 23
OK one more question :-) I am still new to hibernate. Like I said before my app is a swing app so in the HibernateDAOFac I changed the getCurrent session to return HibernateUtil.getCurrentSession();

My question is really more of a "Am I doing this right" I have a class called Service which of cource has a ServiceDAOHibernate. I have a method on Service save() which calls save(Service ser) in my DAO.
The method in the dao looks like this
Code:
   public void save(Service ser) {
      Transaction tx = this.getSession().beginTransaction();
      this.makePersistent(ser);
      tx.commit();
   }


Is this an ok way to utilize Hibernate and the DAO pattern from from the latest Caveat?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 11, 2005 9:52 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
Is this a "method on Service " or a "method in the dao"? If this is a method on the DAO, do not manage the transaction inside the DAO . Otherwise it cannot be used in concert w/ other DAOs. If this is on Service, you are going against the whole point of transparent persistence.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 12, 2005 10:55 am 
Beginner
Beginner

Joined: Sun Dec 05, 2004 10:25 am
Posts: 23
It is on a dao. but the one of the reasons for having to dao is to seperate the persistance layer from the objects themselves. if I put the transaction stuff in my object this will directly tie my object to Hibernate. doesn't this go against the pattern used in the latest CaveatEmptor dao code seen here http://blog.hibernate.org/cgi-bin/blosx ... icdao.html

opinions please


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 12, 2005 12:28 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
i did not say it goes in a business class. transaction management belongs in the controller.

what are you going to do when you need to persist a Service w/ a Banana? you have no way of making the creation of both atomic.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 12, 2005 12:39 pm 
Beginner
Beginner

Joined: Sun Dec 05, 2004 10:25 am
Posts: 23
OK well given the dao pattern from the previos url where would I put the transaction logic. what part of that would be considered the controller. I have object a and object b. both have daos which use the whole dao factory thing. they each have a static method on them that loads an arraylist of themselves. they also each have a save method. In these methods I am getting my dao from the factory and calling the method on teh dao i need to perform the proper action (save, makePersistant, etc...) Given what I am doing where should I put the transaction logic?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 12, 2005 12:49 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
that tutorial isn't about controllers and it would probably lose it's value if he went into detail about Struts Actions or declarative transaction demarcation. Every well written application should have a well defined group of classes that do the logic, in any programming language ;-)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 12, 2005 1:54 pm 
Beginner
Beginner

Joined: Sun Dec 05, 2004 10:25 am
Posts: 23
Well struts is not an option as I am in a Swing app. I would be interested in an example of how I can implement something different here. Do you have an example given the classes I described earlier that would work for seperating this logic.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 12, 2005 1:59 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
they were doing MVC w/ swing long before and of these web frameworks came along. there are plenty of examples on God's home page (google.com) .


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