The Hibernate control provided in jar format by ControlHaus no longer works in
the current version of beehive as the control lifecycle method names have
changed. I have made my own updated version of this which I loosely planned to
open source.
Here is the source I have. This works for my beehive netui web app. No
guarantees or warranty around it's correctness. I'm happy for anyone to review
and provide feedback. I think this would be an asset to provide to the project.
Thanks
Adrian
________________________________
From: José Moreira [mailto:[EMAIL PROTECTED]
Sent: Mon 1/15/2007 10:37 AM
To: [email protected]
Subject: Hibernate Control
Hello,
anyone had experience with the Hibernate Control provided by late
ControlHaus?
Latest release i noticed was an 'alpha2'...
i have a small project i want to implement using Beehive and i'm
deciding on the persistence middleware to use. The JDBC control seems
very simple and easy to use, despite the SQL being inline in the Java code.
thank you and good work.
_______________________________________________________________________
Notice: This email message, together with any attachments, may contain
information of BEA Systems, Inc., its subsidiaries and affiliated
entities, that may be confidential, proprietary, copyrighted and/or
legally privileged, and is intended solely for the use of the individual
or entity named in this message. If you are not the intended recipient,
and have received this message in error, please immediately return this
by email and then delete it.
/**
*
*/
package hibernate.control;
import org.apache.beehive.controls.api.bean.ControlInterface;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
/**
* @author agrealis
*
*/
@ControlInterface
public interface HibernateControl {
public abstract SessionFactory getSessionFactory();
public abstract Session getSession() throws HibernateException;
public abstract Transaction getTransaction();
public abstract void closeSession() throws HibernateException;
}/**
*
*/
package hibernate.control;
import org.apache.beehive.controls.api.bean.ControlImplementation;
import org.apache.beehive.controls.api.context.Context;
import org.apache.beehive.controls.api.context.ControlBeanContext;
import org.apache.beehive.controls.api.context.ResourceContext;
import org.apache.beehive.controls.api.events.EventHandler;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* @author agrealis
*
*/
@ControlImplementation
public class HibernateControlImpl implements HibernateControl, Serializable {
private static final long serialVersionUID = 1L;
private static Logger logger =
Logger.getLogger(HibernateControlImpl.class.getName());
private transient ThreadLocal<Session> session = new
ThreadLocal<Session>();
private transient ThreadLocal<Transaction> transactions = new
ThreadLocal<Transaction>();
private static SessionFactory sessionFactory;
private boolean manageTXs = true;
private static final Configuration cfg = new Configuration();
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
@Context
ControlBeanContext context;
@Context
ResourceContext resourceContext;
public HibernateControlImpl() {
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public Configuration getConfiguration() {
return cfg;
}
@EventHandler(
field="context",
eventSet=ControlBeanContext.LifeCycle.class,
eventName="onCreate"
)
public void onCreate() {
logger.config("onCreate");
if (sessionFactory == null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
sessionFactory = cfg.buildSessionFactory();
} catch (Exception e) {
logger.log(Level.SEVERE, "Couldn't create
session factory!", e);
}
}
}
@EventHandler(
field="resourceContext",
eventSet=ResourceContext.ResourceEvents.class,
eventName="onAcquire"
)
public void onAcquire() {
logger.config("onAcquire");
}
@EventHandler(
field="resourceContext",
eventSet=ResourceContext.ResourceEvents.class,
eventName="onRelease"
)
public void onRelease() {
logger.config("onRelease");
Transaction t = null;
logger.config("Closing open hibernate session.");
try {
if(manageTXs) {
t = getTransaction();
if(t != null) {
t.commit();
}
}
} catch(HibernateException e) {
logger.log(Level.SEVERE, "Couldn't close session!", e);
if(null != t) {
t.rollback();
}
} catch(Exception exception) {
logger.log(Level.SEVERE, "Couldn't close session!", exception);
} finally {
closeSession();
}
}
public Session getSession() throws HibernateException {
Session s = session.get();
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
if (manageTXs)
transactions.set(s.beginTransaction());
}
return s;
}
public Transaction getTransaction() {
return transactions.get();
}
public void closeSession() throws HibernateException {
logger.config("Closing session for thread.");
if (manageTXs)
transactions.remove();
Session s = session.get();
if (s != null) {
session.remove();
s.close();
}
}
}