Java 什么是;“代理”;状态表示休眠会话

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

我在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 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
因此,如果选择ThreadLocaSessionContext,则底层JDBC连接将绑定到线程本地存储,并使其可用于当前线程运行会话

如果使用Spring,则不应依赖Hibernate本地上下文,而应使用特定于Spring的事务管理支持,该支持通过以下方式实现:

  • SpringJtaSessionContext
  • SpringSessionContext
对于代理状态,Hibernate TreadLocalContext为Hibernate会话使用代理:

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() );
}