java sessionmanager_SessionManager.java

package com.htht.common.service;

import java.io.Serializable;

import org.apache.log4j.Logger;

import org.hibernate.HibernateException;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;

/**

* 支持不同数据库用户配置

*

*/

public class SessionManager {

/**

* Location of hibernate.cfg.xml file. Location should be on the classpath

* as Hibernate uses #resourceAsStream style lookup for its configuration

* file. The default classpath location of the hibernate config file is in

* the default package. Use #setConfigFile() to update the location of the

* configuration file for the current session.

*/

//private String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";

private final ThreadLocal sessionThreadLocal = new ThreadLocal();

private final ThreadLocal transactionThreadLocal = new ThreadLocal();

private Configuration configuration = new Configuration();

private SessionFactory sessionFactory;

private String configFile = "";

private Logger logger = Logger.getLogger(HibernateSessionFactory.class);

//static

//{

//try

//{

//configuration.configure(configFile);

//sessionFactory = configuration.buildSessionFactory();

//}

//catch (HibernateException e)

//{

//logger.error(e.toString());

//throw new HibernateException(e);

//}

//}

/**

* 带参构�?

* @param hbmPath 配置文件路径

*/

public SessionManager(String hbmPath)

{

configFile = hbmPath;

try

{

configuration.configure(configFile);

sessionFactory = configuration.buildSessionFactory();

}

catch (HibernateException e)

{

logger.error(e.toString());

throw new HibernateException(e);

}

}

public SessionFactory getSessionFactory()

{

return sessionFactory;

}

public void rebuildSessionFactory()

{

synchronized (sessionFactory)

{

try

{

configuration.configure(configFile);

sessionFactory = configuration.buildSessionFactory();

}

catch (HibernateException e)

{

logger.error(e.toString());

throw new HibernateException(e);

}

}

}

public Session getSession()

{

Session session = (Session) sessionThreadLocal.get();

try

{

if (session == null || !session.isOpen())

{

if (sessionFactory == null)

{

rebuildSessionFactory();

}

session = (sessionFactory != null) ? sessionFactory.openSession() : null;

sessionThreadLocal.set(session);

}

}

catch (HibernateException e)

{

logger.error("HibernateSession FactoryopenSession");

throw new HibernateException(e);

}

return session;

}

public void closeSession()

{

Session session = (Session) sessionThreadLocal.get();

sessionThreadLocal.set(null);

try

{

if (session != null && session.isOpen())

{

session.close();

}

}

catch (HibernateException e)

{

logger.error("HibernateSessionFactory close");

throw new HibernateException(e);

}

}

public void beginTransaction()

{

Transaction transaction = (Transaction) transactionThreadLocal.get();

try

{

if (transaction == null)

{

transaction = getSession().beginTransaction();

transactionThreadLocal.set(transaction);

}

}

catch (HibernateException e)

{

logger.error("HibernateSessionFactory beginTransaction");

throw new HibernateException(e);

}

}

public void commitTransaction()

{

Transaction transaction = (Transaction) transactionThreadLocal.get();

try

{

if (transaction != null && !transaction.wasCommitted() && !transaction.wasRolledBack())

{

transaction.commit();

}

transactionThreadLocal.set(null);

}

catch (HibernateException e)

{

logger.error("HibernateSessionFactory commitTransaction");

throw new HibernateException(e);

}

}

public void rollbackTransaction()

{

Transaction transaction = (Transaction) transactionThreadLocal.get();

try

{

transactionThreadLocal.set(null);

if (transaction != null && !transaction.wasCommitted() && !transaction.wasRolledBack())

{

transaction.rollback();

}

}

catch (HibernateException e)

{

logger.error("HibernateSessionFactory rollbackTransaction");

throw new HibernateException(e);

}

finally

{

closeSession();

}

}

public void setConfigFile(String configFile)

{

//SessionManager.configFile = configFile;

//sessionFactory = null;

}

public Configuration getConfiguration()

{

return configuration;

}

public void add(Object entity)

{

try

{

beginTransaction();

getSession().save(entity);

commitTransaction();

rollbackTransaction();

}

catch (HibernateException e)

{

logger.error("HibernateSessionFactory add");

throw new HibernateException(e);

}

}

public void update(Object entity)

{

try

{

beginTransaction();

getSession().update(entity);

commitTransaction();

rollbackTransaction();

}

catch (HibernateException e)

{

logger.error("HibernateSessionFactory update");

throw new HibernateException(e);

}

}

public void delete(Object entity)

{

try

{

beginTransaction();

getSession().delete(entity);

commitTransaction();

rollbackTransaction();

}

catch (HibernateException e)

{

logger.error("HibernateSessionFactory delete");

throw new HibernateException(e);

}

}

public Object get(Class> clazz, Serializable id)

{

Object object = null;

try

{

object = getSession().get(clazz, id);

}

catch (HibernateException e)

{

logger.error("HibernateSessionFactory get");

throw new HibernateException(e);

}

finally

{

closeSession();

}

return object;

}

}

一键复制

编辑

Web IDE

原始数据

按行查看

历史

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值