2010-11-14 3 views
2

Essayer de faire fonctionner la gestion des transactions de Spring, mais ça ne va pas comme je l'espérais.Spring & Hibernate: aucune session liée au thread

je reçois une exception quand tout demande qui nécessite ma base de données:

DEBUG: org.springframework.orm.hibernate3.SessionFactoryUtils - Opening Hibernate Session 
DEBUG: org.springframework.orm.hibernate3.SessionFactoryUtils - Opening Hibernate Session 
DEBUG: org.hibernate.impl.SessionImpl - opened session at timestamp: 12897642913 
DEBUG: org.springframework.orm.hibernate3.SessionFactoryUtils - Closing Hibernate Session 
DEBUG: org.springframework.orm.hibernate3.SessionFactoryUtils - Closing Hibernate Session 
14-nov-2010 20:51:31 org.apache.catalina.core.StandardWrapperValve invoke 
SEVERE: Servlet.service() for servlet mvc-dispatcher threw exception 
org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here 

je l'ai déjà déplacé mes propriétés à mon contexte de printemps pour voir si cela allait mieux, mais pas. Ma configuration:

<?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:aop="http://www.springframework.org/schema/aop" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:oxm="http://www.springframework.org/schema/oxm" 
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
     http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> 

    <bean id="myDataSource" 
     class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
     <property name="url" value="jdbc:mysql://localhost/kidscalcula" /> 
     <property name="username" value="root" /> 
     <property name="password" value="" /> 
    </bean> 

    <bean class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" 
     id="sessionFactory"> 
     <property name="dataSource" ref="myDataSource" /> 
     <property name="mappingResources"> 
      <list> 
       <value>be/howest/kidscalcula/model/Foto.hbm.xml</value> 
       <value>be/howest/kidscalcula/model/Kindleerplanonderdeel.hbm.xml 
       </value> 
       <value>be/howest/kidscalcula/model/Klas.hbm.xml</value> 
       <value>be/howest/kidscalcula/model/Leerkracht.hbm.xml</value> 
       <value>be/howest/kidscalcula/model/Leerling.hbm.xml</value> 
       <value>be/howest/kidscalcula/model/Leerplan.hbm.xml</value> 
       <value>be/howest/kidscalcula/model/LeerplanOefenreeks.hbm.xml 
       </value> 
       <value>be/howest/kidscalcula/model/Leerplanonderdeel.hbm.xml</value> 
       <value>be/howest/kidscalcula/model/Niveau.hbm.xml</value> 
       <value>be/howest/kidscalcula/model/Oefenreeks.hbm.xml</value> 
       <value>be/howest/kidscalcula/model/Overgangsregel.hbm.xml</value> 
       <value>be/howest/kidscalcula/model/Rapport.hbm.xml</value> 
       <value>be/howest/kidscalcula/model/RapportLeerplanonderdeel.hbm.xml 
       </value> 
       <value>be/howest/kidscalcula/model/Schooljaar.hbm.xml</value> 
       <value>be/howest/kidscalcula/model/Subonderdeel.hbm.xml</value> 
      </list> 
     </property> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
       <prop key="hibernate.hbm2ddl.auto">update</prop> 
       <prop key="hibernate.connection.pool_size">3</prop> 
       <prop key="hibernate.show_sql">true</prop> 
       <prop key="hibernate.format_sql">true</prop> 
       <prop key="hibernate.use_sql_comments">true</prop> 
       <prop key="hibernate.cache.use_second_level_cache">false</prop> 
      </props> 
     </property> 
    </bean> 


    <!-- Transaction manager for a single Hibernate SessionFactory (alternative 
     to JTA) --> 
    <bean id="transactionManager" 
     class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
     <property name="sessionFactory"> 
      <ref bean="sessionFactory" /> 
     </property> 
    </bean> 

    <tx:annotation-driven /> 
</beans> 

J'injectent juste mes org.hibernate.SessionFactory dans mon DAO et utiliser @Transactional annotations sur mes méthodes ou sur ma classe.

@Repository 
public class LeerlingDAOimpl implements LeerlingDAO { 
    @Autowired 
    public LeerlingDAOimpl(SessionFactory sessionFactory) { 
     this.sessionFactory = sessionFactory; 
    } 

Est-ce que quelqu'un a une idée de ce que j'ai oublié, mal configuré? L'idée de base est qu'avec cette configuration normalement, une session est ouverte chaque fois qu'une méthode transactionnelle est appelée dans ma couche de service. Cela me permettrait également de charger des collections paresseuses dans cette même méthode transactionnelle. Mais pour une raison quelconque, il ne trouve même pas le fil.

+0

Obtenez le stacktrace pour l'exception. Le TransactionInterceptor apparaît-il? – meriton

+3

J'ai trouvé le problème. Mon analyse d'annotation s'est déroulée dans un autre fichier xml, puis mon . Il n'a jamais découvert qu'il devait démarrer une transaction, donc il n'y a jamais eu de session trouvée. – toomuchcs

Répondre

7

Depuis votre configuration semble bien, il y a plusieurs raisons possibles:

  • Vous appelez la méthode @Transactional sur l'objet créé avec new (non obtenu à partir du printemps)

  • Vous appelez @Transactional méthode d'une autre méthode du même objet (dans ce cas, l'aspect transactionnel n'est pas appliqué car il est basé sur un proxy)

  • Votre objet avec @Transactional méthodes est déclarée dans le contexte où <tx:annotation-driven> est pas en vigueur (par exemple, votre objet @Transactional est déclaré dans ...-servlet.xml, alors que <tx:annotation-driven> est seulement déclarée dans applicationContext.xml)

+0

il est appelé à partir d'un contrôleur Spring MVC, où le service est injecté. Aucune des deux premières possibilités ne s'applique. Quant au troisième: ça pourrait être ça! Je vais le tester. cependant, je n'ai pas divisé les configurations comme le recommande le printemps; juste un root-context.xml qui importe 3 autres xml, mon répartiteur de servlets, hibernateconfiguration et un autre pour JSON. – toomuchcs

+0

merci beaucoup. Je me suis cassé la tête sur cette question pendant environ 2 jours. Je n'ai jamais pensé que quelque chose d'aussi trivial serait ça! Mais je suis content, j'ai appris autre chose au printemps que je n'oublierai jamais;) – toomuchcs

Questions connexes