2011-09-21 9 views
9

Ho trascorso alcuni giorni in un problema di transazione con letargo primaverile. Creo una semplice webservice con jaxws + molla + Hibernate, funziona benissimo, ma quando chiamo un Methode web che utilizzano una molla di fagioli transazionale ha gettato il seguente errore:Eccezione Spring/Hibernate: createCriteria non è valida senza transazione attiva

21 sept. 2011 14:29:29 com.sun.xml.ws.server.sei.EndpointMethodHandler invoke 
GRAVE: org.hibernate.HibernateException: createCriteria is not valid without active transaction 

mi sembra sia stato avviato l'operazione ... ma è successo qualcosa di sbagliato.

[jmedia] 21 sept. 2011 14:29:29 [http-8080-1] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager - Creating new transaction with name [com.cellfish.mediadb.ws.encoder.MediaDBFeeds.testTransaction]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly; '' 
[jmedia] 21 sept. 2011 14:29:29 [http-8080-1] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager - Opened new Session [[email protected]] for Hibernate transaction 
[jmedia] 21 sept. 2011 14:29:29 [http-8080-1] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager - Preparing JDBC Connection of Hibernate Session [[email protected]] 
[jmedia] 21 sept. 2011 14:29:29 [http-8080-1] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager - Exposing Hibernate transaction as JDBC transaction [jdbc:mysql://xxxxxxxx, MySQL-AB JDBC Driver] 
[jmedia] 21 sept. 2011 14:29:29 [http-8080-1] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager - Found thread-bound Session [[email protected]] for Hibernate transaction 
[jmedia] 21 sept. 2011 14:29:29 [http-8080-1] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager - Participating in existing transaction 
[jmedia] 21 sept. 2011 14:29:29 [http-8080-1] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager - Participating transaction failed - marking existing transaction as rollback-only 
[jmedia] 21 sept. 2011 14:29:29 [http-8080-1] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager - Setting Hibernate transaction on Session [[email protected]] rollback-only 
[jmedia] 21 sept. 2011 14:29:29 [http-8080-1] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager - Initiating transaction rollback 
[jmedia] 21 sept. 2011 14:29:29 [http-8080-1] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager - Rolling back Hibernate transaction on Session [[email protected]] 
[jmedia] 21 sept. 2011 14:29:29 [http-8080-1] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager - Closing Hibernate Session [[email protected]] after transaction 
21 sept. 2011 14:29:29 com.sun.xml.ws.server.sei.EndpointMethodHandler invoke 

Qui il mio applicationContext:

<context:annotation-config/> 

    <!-- List of packages managed by Spring --> 
    <context:component-scan base-package="..." /> 

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
     <property name="driverClassName" value="com.mysql.jdbc.Driver"/> 
     <property name="url" value="jdbc:mysql://xxxx"/> 
     <property name="username" value="xxx"/> 
     <property name="password" value="xxx"/> 
    </bean> 

    <bean id="sessionFactory" 
     class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
     <property name="packagesToScan" value="xxxx"/> 
     <property name="dataSource" ref="dataSource"/> 
     <property name="configLocation" value="classpath:hibernate.cfg.xml" /> 
    </bean> 

    <tx:annotation-driven transaction-manager="transactionManager" /> 
    <bean id="transactionManager" 
     class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="sessionFactory" /> 
    </bean> 

E il hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-configuration PUBLIC 
"-//Hibernate/Hibernate Configuration DTD//EN" 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
    <!-- a SessionFactory instance listed as /jndi/name --> 
    <session-factory> 

     <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property> 
     <property name="show_sql">true</property> 
     <property name="hibernate.format_sql">true</property> 
     <property name="hibernate.connection.pool_size">1</property> 

     <property name="hibernate.jdbc.batch_size">20</property> 
     <!-- Bind the getCurrentSession() method to the thread. --> 
     <property name="current_session_context_class">thread</property> 
     <property name="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</property> 
     <property name="hibernate.cache.use_second_level_cache">true</property> 
     <property name="hibernate.cache.use_query_cache">true</property> 

     <!-- Lucene Search --> 
     <property name="hibernate.search.default.directory_provider">org.hibernate.search.store.RAMDirectoryProvider</property> 

    </session-factory> 

ho schierato questa webapp su tomcat6. Spero che tu possa aiutarmi a risolvere questo problema.

Cheers

+0

giusto per essere sicuri - il bean/metodo ha un'annotazione @Transactional? E lo stesso bean è chiamato e gestito in un contesto primaverile? –

+0

sì il mio bean è annoted con @Transactional e gestito da Spring. – juliusdev

risposta

25

Ok ho trovato il problema! Ho rimosso questa linea dalla configurazione di ibernazione. Spring gestisce la transazione e non è necessario che la sessione sia trattenuta in un thread di sospensione.

<!-- Bind the getCurrentSession() method to the thread. --> 
<property name="current_session_context_class">thread</property> 
Problemi correlati