这个问题是为了提高我对如何在春季休眠中的会话中管理事务的理解。当我打印 getSessionFactory()getStatistics().getTransactionCount() 时,当 getSessionFactory().getCurrentSession().getTransaction().isActive() 为“真”时,它会打印“0”。我真的很困惑。请为我清除同样的问题。
以下是我希望交易发生的一段代码:
@Transactional(propagation=Propagation.REQUIRED)
public int insertNewRecord(){
System.out.println(getSessionFactory().getCurrentSession().getTransaction().isActive());//gives `true`
System.out.println(getSessionFactory().getCurrentSession().getStatistics()); //Gives statistics
System.out.println(getSessionFactory().getStatistics().getSessionOpenCount());// Gives 1
System.out.println(getSessionFactory().getStatistics().getTransactionCount());// Gives 0
}
我的 spring 和 Hibernate 配置:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingLocations">
<list><value>classpath:Employee.hbm.xml</value></list>
</property>
<property name="hibernateProperties">
<props>
<!-- SQL dialect -->
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<!-- Enable Hibernate's current session context -->
<!-- <prop key="hibernate.current_session_context_class">org.hibernate.context.internal.ThreadLocalSessionContext</prop> --><!-- org.hibernate.context.internal.ThreadLocalSessionContext -->
<!-- Disable the second-level cache -->
<prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
<!-- Echo all executed SQL to stdout -->
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.generate_statistics">true</prop>
</props>
</property>
</bean>
下面是事务管理器:
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="dataSource" ref="dataSource" />
<property name="sessionFactory" ref="sessionFactory" />
</bean>