Java 什么是;“代理”;状态表示休眠会话
我在Jboss站点上遇到了关于Hibernate文档的这一行Java 什么是;“代理”;状态表示休眠会话,java,hibernate,orm,proxy,transactions,Java,Hibernate,Orm,Proxy,Transactions,我在Jboss站点上遇到了关于Hibernate文档的这一行 Because Hibernate can't bind the "current session" to a transaction, as it does in a JTA environment, it binds it to the current Java thread when i do transction demarcation with plain JDBC. It is opened when getCurre
Because Hibernate can't bind the "current session" to a transaction,
as it does in a JTA environment, it binds it to the current Java thread
when i do transction demarcation with plain JDBC.
It is opened when getCurrentSession() is called for the first time,
but in a "proxied" state that doesn't allow you to do anything except
start a transaction.
那么,作者在这里所说的“代理状态”究竟是什么意思呢。以及它们与代理对象的链接(如果有的话)?如果没有JTA,事务管理是通过JTA的提交/回滚方法完成的 这意味着您必须将一个JDBC连接绑定到当前运行的Hibernate会话和当前逻辑事务 因为将JDBC连接传递给所有Hibernate会话方法将是一个糟糕的设计解决方案,所以必须使用线程本地存储 Hibernate有一个灵活的解决方案,提供以下替代方案:
- JTASessionContext
- ManagedSessionContext
- ThreadLocalSessionContext
- SpringJtaSessionContext
- SpringSessionContext
protected Session wrap(Session session) {
final TransactionProtectionWrapper wrapper = new TransactionProtectionWrapper( session );
final Session wrapped = (Session) Proxy.newProxyInstance(
Session.class.getClassLoader(),
SESSION_PROXY_INTERFACES,
wrapper
);
wrapper.setWrapped( wrapped );
return wrapped;
}
调用Session.close()方法时,允许当前会话从本地存储中解除绑定
// If close() is called, guarantee unbind()
if ( "close".equals( methodName ) ) {
unbind( realSession.getSessionFactory() );
}