他们建议在Java EE环境中使用JTA事务支持.
但是如何在Tomcat6中配置JTA以便Hibernate Session可以使用它呢?
从版本3.0.1开始,Hibernate添加了该
SessionFactory.getCurrentSession()方法.最初,这假定使用JTA事务,其中JTA事务定义了当前会话的范围和上下文.鉴于众多独立JTA TransactionManager实现的成熟度,大多数(如果不是全部)应用程序应该使用JTA事务管理,无论它们是否部署到J2EE容器中.基于此,您需要使用基于JTA的上下文会话.
我有一个使用Hibernate 4和Spring Transactions的Spring 3.2应用程序.所有方法都运行良好,我可以正确访问数据库以保存或检索实体.然后,我介绍了一些多线程,并且由于每个线程都访问了db,我从Hibernate收到以下错误:
org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
Run Code Online (Sandbox Code Playgroud)
我从网上读到我要添加<prop key="hibernate.current_session_context_class">thread</prop>到我的Hibernate配置中,但是现在每次我尝试访问db时都会得到:
org.hibernate.HibernateException: saveOrUpdate is not valid without active transaction
Run Code Online (Sandbox Code Playgroud)
但是我的服务方法是注释的@Transactional,并且在添加之前一切正常<prop key="hibernate.current_session_context_class">thread</prop>.
为什么没有交易,尽管方法是用@Transactional注释的?我怎么解决这个问题?
这是我的Hibernate配置(包括会话上下文属性):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<!-- Hibernate session factory -->
<bean
id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
<property name="dataSource" >
<ref bean="dataSource" />
</property>
<property name="hibernateProperties" >
<props>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
</props>
</property>
<property name="annotatedClasses" …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Spring 3.1和Hibernate 4来设置我的项目.我一直在线学习一些教程.我得到一个奇怪的错误,根据Spring论坛应该已经修复了Spring 3.1. 春虫追踪器
当我的服务调用时getCurrentSession(),它会抛出以下异常:
org.hibernate.HibernateException: **No Session found for current thread**] with root cause org.hibernate.HibernateException: No Session found for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97) at
org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:881)
Run Code Online (Sandbox Code Playgroud)
****编辑:根据Spring Spring 3.1 Transactions for Transactions更新了我的spring-dao.xml .我尝试使用org.apache.commons.dbcp.BasicDataSource替换我的数据源.我的配置中是否有任何可能导致此问题的属性?****
这是我的spring-dao.xml:
<!-- Enable annotation style of managing transactions -->
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<value>hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect</value>
</property>
</bean>
<!-- Declare a datasource that has pooling capabilities-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close"
p:driverClass="${app.jdbc.driverClassName}"
p:jdbcUrl="${app.jdbc.url}"
p:user="${app.jdbc.username}"
p:password="${app.jdbc.password}"
p:acquireIncrement="5"
p:idleConnectionTestPeriod="60"
p:maxPoolSize="100" …Run Code Online (Sandbox Code Playgroud) 我想知道current_session_context_classcfg.xml文件中可以与属性一起使用的所有可能值吗?我有一个线程值的想法,使会话上下文与每个线程相关,就像propertyname="current_session_context_class"线程一样.