protected boolean close = true;
protected String standardTransactionName;
protected String connectionName;
public Object construct(WireContext wireContext) {
Environment environment = Environment.getCurrent();
if (environment==null) {
throw new WireException("no environment");
}
// get the hibernate-session-factory
SessionFactory sessionFactory = null;
if (factoryName!=null) {
sessionFactory = (SessionFactory) wireContext.get(factoryName);
} else {
sessionFactory = environment.get(SessionFactory.class);
}
if (sessionFactory==null) {
throw new WireException("couldn't find hibernate-session-factory "+(factoryName!=null ? "'"+factoryName+"'" : "by type ")+"to open a hibernate-session");
}
// open the hibernate-session
Session session = null;
if (useCurrent) {
if (log.isTraceEnabled()) log.trace("getting current hibernate session");
session = sessionFactory.getCurrentSession();
} else if (connectionName!=null) {
Connection connection = (Connection) wireContext.get(connectionName);
if (log.isTraceEnabled()) log.trace("creating hibernate session with connection "+connection);
session = sessionFactory.openSession(connection);
} else {
if (log.isTraceEnabled()) log.trace("creating hibernate session");
session = sessionFactory.openSession();
}
StandardTransaction standardTransaction = environment.get(StandardTransaction.class);
if (standardTransaction!=null) {
HibernateSessionResource hibernateSessionResource = new HibernateSessionResource(session);
standardTransaction.enlistResource(hibernateSessionResource);
}