Hibernate3.3.2版本中getSession().connection()已被弃用,替代方法SessionFactoryUtils.getDataSource(getSessionFactory()).getConnection()
来自类org.springframework.orm.hibernate3.SessionFactoryUtils
例子:
public
class
SqlServiceImpl
extends
HibernateDaoSupport {
Connection connection = null;
PreparedStatement statement = null;
public int exeSQL(final String sql) {
return (Integer) super.getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
connection = SessionFactoryUtils.getDataSource(
session.getSessionFactory()).getConnection();
// connection=session.connection();
statement = connection.prepareStatement(sql);
return statement.executeUpdate();
}
});
}
}
注:
1、关闭connection、statement连接
2、在applictioncontext.xml文件中配置数据源DataSource
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- 其它配置信息-->
</bean>