私は、Hibernate 4とSpringのトランザクションを使用するSpring 3.2アプリケーションを持っています。すべてのメソッドがうまくいき、エンティティを保存または取得するためにデータベースに正しくアクセスできました。 はその後、私はいくつかのマルチスレッドを導入し、各スレッドは、私が休止状態から、次のエラーを取得したDBへアクセスしていたので、:Springのトランザクションとhibernate.current_session_context_class
org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
私は私が私のHibernate設定に<prop key="hibernate.current_session_context_class">thread</prop>
を追加しましたウェブから読み込まれますが、今私は私が手デシベルアクセスしようとするたびに:
org.hibernate.HibernateException: saveOrUpdate is not valid without active transaction
は、しかし、私のサービスメソッドは@Transactional
でアノテートされ、すべてが<prop key="hibernate.current_session_context_class">thread</prop>
の追加の前に罰金働いていました。
@Transactionalでアノテーションされているにもかかわらず、トランザクションが存在しないのはなぜですか?どうすればこの問題を解決できますか?
<?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" >
<list>
...
</list>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
私は以前の設定を元に戻しましたが、今は 'org.hibernate.HibernateException:マルチスレッドに付属する2つのオープンセッションとコレクションを関連付ける試みが不正です。基本的に私はスレッドにする必要があります、そして、彼らはコレクションを含む同じオブジェクトに対して 'save()'操作をしようとします。たぶん私はこの状況を避け、それらのうちの1つだけをしなければならないでしょうか? – user1781028
なぜ、複数のスレッドによって保存されている単一のオブジェクトを持っていますか? –
Ok保存操作の1つを削除しました。しかし、何らかの理由で本当に両方が必要な場合はどうしたらいいですか? – user1781028