根据文档(Spring 文档),它只是元数据,表明方法或接口可以由“事务感知”的东西配置(即
只有 tx:annotation-driven 而没有 @Transactional 属性,我相信您会应用“默认”事务性:
传播设置是必需的。
隔离级别为默认。
事务是读/写的。
事务超时默认为底层事务系统的默认超时,如果不支持超时,则为无。
ny RuntimeException 会触发回滚,而任何已检查的异常都不会。
假设您使用 tx:annotation 通过事务管理器驱动它,那么缺少 @Transactional 属性意味着您不能应用诸如只读、隔离、传播、rollbackFor、noRollbackFor 等属性。
我相信 MVC 略有不同 - 休眠会话直接绑定到 MVC 请求 - 即,当收到请求时,事务开始。
回到你的例子,HibernateDAOSupport中getSession()的代码如下:
protected final Session getSession()
throws DataAccessResourceFailureException, IllegalStateException
{
return getSession(this.hibernateTemplate.isAllowCreate());
}
依次调用:
/**
* 从当前事务或获取 Hibernate Session
* 一个新的。仅当“allowCreate”为真时才允许使用后者。
*.......
protected final Session getSession()
throws DataAccessResourceFailureException, IllegalStateException {
return getSession(this.hibernateTemplate.isAllowCreate());
}
最终调用:
/**
* ....
* @param allowCreate 是否应该创建一个非事务性会话
* 当当前线程找不到事务会话时
* ....
*/
private static Session doGetSession(
SessionFactory sessionFactory, Interceptor entityInterceptor,
SQLExceptionTranslator jdbcExceptionTranslator, boolean allowCreate)
从根本上说,Transaction:Session 是 1:1 afaik 绑定的,而在没有事务的情况下运行的唯一方法是使用 say JBoss,它有一个“烘焙”的持久层,为您提供事务性(在幕后) .即使您在 getSession() 之后调用 getQuery(),您仍然有效地发生事务,因为它是 JDBC/Hibernate 连接。