atitit.擷取connection hibernate4,atitithibernate4
atitit.擷取connection hibernate4
1. SessionFactoryUtils法(推薦) 1
2. ConnectionProvider 法( ) 1
3. 嘎自實現法(不推薦)讀取hb設定檔 1
4. 或者使用work (但是work黑頭的conn是個閉包,,不好用) 1
1. SessionFactoryUtils法(推薦)
這個方法產生的多個conn實際都是一個,所以,不好用
Hibernate3.3.2版本中getSession().connection()已被棄用,替代方法SessionFactoryUtils.getDataSource(getSessionFactory()).getConnection()
作者:: 老哇的爪子 Attilax 艾龍, EMAIL:1466519819@qq.com
轉載請註明來源: http://blog.csdn.net/attilax
2. ConnectionProvider 法( )
雅十,這個方法產生的多個conn實際都是一個,所以,不好用
ConnectionProvider cp = ((SessionFactoryImplementor)ssn.getSessionFactory()).getConnectionProvider();
3. 嘎自實現法(不推薦)讀取hb設定檔
讀取hb設定檔,通過jdbc api產生con
c=new Hb4JdbcX().getConnection();
wechat4propty 項目..
// c = cp.getConnection();
4. 或者使用work (但是work黑頭的conn是個閉包,,不好用)
bd.bind(Connection.class).toInstance(
(Connection) new Closure2() {
@Override
public Object execute(Object arg0) {
BaseImpl basedao = new BaseImpl();
SessionFactory sessionFactory = HibernateSessionFactory.getSessionFactory();
Connection c;
try {
c = SessionFactoryUtils.getDataSource(sessionFactory ).getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e);
}
return c;
}
}.execute(null));
Hibernate Session 擷取connection - liuxianbing119的專欄 - 部落格頻道 - CSDN.NET.htm