如果我有一个Spring Transaction托管的DataSource,并使用它来提供与Hibernate无状态会话的连接,我是否需要关闭无状态会话?如果是这样,关闭数据库连接和提交数据库事务会不会是一个noop?
这就是我现在所拥有的:
import java.sql.Connection;
import javax.sql.DataSource;
import org.hibernate.SessionFactory;
import org.hibernate.StatelessSession;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceUtils;
...
@Autowired
private SessionFactory sessionFactory;
@Autowired
private DataSource dataSource;
public MyObj getCurrentByName(String name)
{
StatelessSession session = null;
try
{
Connection conn = DataSourceUtils.getConnection(dataSource);
session = sessionFactory.withStatelessOptions()
.connection(conn)
.openStatelessSession();
return (MyObj) session.createCriteria(MyObj.class)
.add(Restrictions.eq("isCurrent", true))
.add(Restrictions.eq("name", name))
.setComment("MyObj by name")
.uniqueResult();
}
finally
{
if (session != null)
{
session.close();
}
}
}
再次,session.close()是否只是清理会话对象?即不提交txn也不关闭数据库连接?