Spring Transactions and hibernate.current_session_context_class -


i have spring 3.2 application uses hibernate 4 , spring transactions. methods working great , access correctly database save or retrieve entities. then, introduced multithreading, , since each thread accessing db getting following error hibernate:

org.hibernate.hibernateexception: illegal attempt associate collection 2 open sessions 

i read web i've add <prop key="hibernate.current_session_context_class">thread</prop> hibernate configuration, every time try access db get:

org.hibernate.hibernateexception: saveorupdate not valid without active transaction 

however service methods annotated @transactional, , working fine before add of <prop key="hibernate.current_session_context_class">thread</prop>.

why there no transaction although methods annotated @transactional? how can solve problem?

here hibernate configuration (including session context property):

<?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"/> 

when using spring , spring managed transactions never mess around hibernate.current_session_context_class property unless using jta.

spring default set own currentsessioncontext implementation (the springsessioncontext), if set not case. breaking proper transaction integration.

the reason chancing setting if want use jta managed transactions, have setup integrate jta.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -