📄 hibernatesession.java
字号:
package dbdemo.util.hibernate;
import java.sql.Connection;
import java.sql.SQLException;
import org.apache.log4j.Logger;
import cirrus.hibernate.Datastore;
import cirrus.hibernate.Hibernate;
import cirrus.hibernate.HibernateException;
import cirrus.hibernate.MappingException;
import cirrus.hibernate.Session;
import cirrus.hibernate.SessionFactory;
import dbdemo.User;
import dbdemo.Address;
import dbdemo.Book;
import dbdemo.Contact;
import dbdemo.exception.HibernateException;
import dbdemo.portal.UserContainer;
import dbdemo.util.DatasourceFactory;
/**
* @author MEagle
*
* This is for Hibernate version 1.x
*
* Hibernate is designed to be useable in any kind of Java application,
* including applications that make extensive use of multithreading. So,
* unlike the ODMG API, Hibernate's native API does not use the current
* thread to maintain associations between Session and Transaction or
* between Session and application thread. This can be inconvenient for
* J2EE applications where access to a Session instance is always from
* a particular thread. It is particularly inconvenient when using a DAO
* pattern, when the different DAO classes need to obtain the (same)
* current session.
*
*
* A thread-local variable effectively provides a separate copy of its
* value for each thread that uses it. Each thread can see only the value
* associated with that thread, and is unaware that other threads may be
* using or modifying their own copies.
*/
public class HibernateSession {
// Add a Log4J logger
static Logger log = Logger.getLogger(HibernateSession.class.getName());
static Datastore dataStore = null;
static SessionFactory sf = null;
static {
try {
init();
} catch (HibernateException e) {
log.error("could not init " + e.getMessage());
}
}
public static final ThreadLocal local = new ThreadLocal();
/**
* Retrieve the current Hibernate Session object via the ThreadLocal
* class.
*
* @param uc - the user's container object stored in the session
* @return Session
* @throws HibernateException
*
*/
public static Session currentSession(UserContainer uc)
throws HibernateException {
Session session = currentSession();
log.info(
session
+ " "
+ uc.getFullName()
+ " HibernateSession:currentSession");
return session;
}
/**
* Retrieve the current Hibernate Session object via the ThreadLocal
* class.
*
* @return Session
* @throws HibernateException
*
*/
public static Session currentSession() throws HibernateException {
Session session = (Session) local.get();
if (session == null) {
Connection con = null;
// create a datasource object
if (sf == null) {
init();
}
try {
// get a connnection from a pool
con = DatasourceFactory.getConnection();
// create a session object to the database
session = sf.openSession(con);
} catch (SQLException sqle) {
log.error("SQLException: " + sqle.getMessage());
throw new HibernateException("SQLException: " + sqle.getMessage());
}
local.set(session);
}
return session;
}
/**
* Close the Hibernate Session and any underlying Connection
* for further cleanup
*
* @param uc - the user's container object stored in the session
* @throws HibernateException, SQLException
*/
public static void closeSession(UserContainer uc)
throws HibernateException, SQLException {
String hashCode = closeSession();
log.info(
hashCode
+ " "
+ uc.getFullName()
+ " HibernateSession:closeSession");
}
/**
* Close the Hibernate Session and any underlying Connection
* for further cleanup
*
* @param uc - the user's container object stored in the session
* @return String - the hashcode of the session object that was closed
* for debug purposes
* @throws HibernateException, SQLException
*/
public static String closeSession()
throws HibernateException, SQLException {
Connection con = null;
Session session = (Session) local.get();
String sessionHashCode = session.toString();
local.set(null);
if (session != null) {
con = session.close();
}
if (con != null) {
con.close();
}
return sessionHashCode;
}
private static synchronized void init() throws HibernateException {
try {
dataStore = Hibernate.createDatastore();
// load up the different configurations
dataStore
.storeClass(User.class)
.storeClass(Address.class)
.storeClass(Contact.class)
.storeClass(Book.class);
sf = dataStore.buildSessionFactory();
System.out.println(
"HibernateSession Initialized SessionFactory=" + sf);
} catch (MappingException me) {
log.error(
"Could not intialize Hibernate session factory. "
+ me.getMessage());
throw new HibernateException(
"Could not intialize Hibernate session factory. "
+ me.getMessage());
} catch (HibernateException he) {
log.error(
"Could not intialize Hibernate session factory. "
+ he.getMessage());
throw new HibernateException(
"Could not intialize Hibernate session factory. "
+ he.getMessage());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -