2012-09-13 43 views
0

我不得不將Spring WebFlow項目更新到Spring 3.1,並且Hibernate 4試圖讓Maven工作。看起來一切都在現在的工作,但在項目要做一個數據庫的一部分讀我收到以下錯誤:已更新Spring 3.1項目到hibernate4現在正在爲當前線程找到沒有會話

org.hibernate.HibernateException: No Session found for current thread org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97) 

我不知道爲什麼我沒有在Hibernate3得到這個錯誤,但有人可以告訴我如何解決這個問題。

這裏是我的pom.xml

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>org.springframework.samples</groupId> 
    <artifactId>webflow-primefaces-showcase</artifactId> 
    <name>JSF 2, Spring Web Flow, and PrimeFaces Showcase</name> 
    <packaging>war</packaging> 
    <version>1.0.0-BUILD-SNAPSHOT</version> 

    <properties> 
     <java-version>1.5</java-version> 
     <springframework-version>3.1.1.RELEASE</springframework-version> 
     <springwebflow-version>2.3.1.BUILD-SNAPSHOT</springwebflow-version> 
     <springsecurity-version>3.1.1.RELEASE</springsecurity-version> 
     <org.slf4j-version>1.5.10</org.slf4j-version> 
     <hibernate.version>4.1.1.Final</hibernate.version> 
    </properties> 

    <dependencies> 
     <!-- Spring --> 

     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-orm</artifactId> 
      <version>${springframework-version}</version> 
      <!-- will come with all needed Spring dependencies such as spring-core 
       and spring-beans --> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-aspects</artifactId> 
      <version>${springframework-version}</version> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-webmvc</artifactId> 
      <version>${springframework-version}</version> 
      <exclusions> 
       <exclusion> 
        <groupId>commons-logging</groupId> 
        <artifactId>commons-logging</artifactId> 
       </exclusion> 
      </exclusions> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.webflow</groupId> 
      <artifactId>spring-faces</artifactId> 
      <version>${springwebflow-version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-core</artifactId> 
      <version>${springsecurity-version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-config</artifactId> 
      <version>${springsecurity-version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-web</artifactId> 
      <version>${springsecurity-version}</version> 
     </dependency> 



     <dependency> 
      <groupId>javax.transaction</groupId> 
      <artifactId>jta</artifactId> 
      <version>1.1</version> 
     </dependency> 
     <dependency> 
      <groupId>log4j</groupId> 
      <artifactId>log4j</artifactId> 
      <version>1.2.14</version> 
     </dependency> 

     <dependency> 
      <groupId>cglib</groupId> 
      <artifactId>cglib</artifactId> 
      <version>2.2.2</version> 
     </dependency> 

     <dependency> 
      <groupId>mysql</groupId> 
      <artifactId>mysql-connector-java</artifactId> 
      <version>5.1.21</version> 
     </dependency> 

     <dependency> 
      <groupId>c3p0</groupId> 
      <artifactId>c3p0</artifactId> 
      <version>0.9.1.2</version> 
     </dependency> 

     <dependency> 
      <groupId>org.apache.httpcomponents</groupId> 
      <artifactId>httpcore</artifactId> 
      <version>4.2.1</version> 
     </dependency> 

     <dependency> 
      <groupId>org.apache.httpcomponents</groupId> 
      <artifactId>httpclient</artifactId> 
      <version>4.2.1</version> 
      <exclusions> 
       <exclusion> 
        <artifactId>commons-logging</artifactId> 
        <groupId>commons-logging</groupId> 
       </exclusion> 
      </exclusions> 
     </dependency> 

     <dependency> 
      <groupId>org.aspectj</groupId> 
      <artifactId>aspectjtools</artifactId> 
      <version>1.6.2</version> 
     </dependency> 

     <dependency> 
      <groupId>jstl</groupId> 
      <artifactId>jstl</artifactId> 
      <version>1.2</version> 
     </dependency> 

     <!-- Logging --> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-api</artifactId> 
      <version>${org.slf4j-version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>jcl-over-slf4j</artifactId> 
      <version>${org.slf4j-version}</version> 
      <scope>runtime</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-log4j12</artifactId> 
      <version>${org.slf4j-version}</version> 
      <scope>runtime</scope> 
     </dependency> 
     <dependency> 
      <groupId>log4j</groupId> 
      <artifactId>log4j</artifactId> 
      <version>1.2.16</version> 
      <scope>runtime</scope> 
     </dependency> 
     <!-- JSF-303 Dependency Injection --> 
     <dependency> 
      <groupId>javax.inject</groupId> 
      <artifactId>javax.inject</artifactId> 
      <version>1</version> 
     </dependency> 
     <!-- Servlet --> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>servlet-api</artifactId> 
      <version>2.5</version> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet.jsp</groupId> 
      <artifactId>jsp-api</artifactId> 
      <version>2.1</version> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>jstl</artifactId> 
      <version>1.2</version> 
     </dependency> 
     <!-- Sun Mojarra JSF 2 runtime --> 
     <dependency> 
      <groupId>com.sun.faces</groupId> 
      <artifactId>jsf-api</artifactId> 
      <version>2.1.7</version> 
     </dependency> 
     <dependency> 
      <groupId>com.sun.faces</groupId> 
      <artifactId>jsf-impl</artifactId> 
      <version>2.1.7</version> 
     </dependency> 


     <!-- JSR 303 validation --> 
     <dependency> 
      <groupId>javax.validation</groupId> 
      <artifactId>validation-api</artifactId> 
      <version>1.0.0.GA</version> 
     </dependency> 

     <!-- ********************************************************************** 
      ** HIBERNATE DEPENDENCIES ** ********************************************************************** --> 

     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-entitymanager</artifactId> 
      <version>${hibernate.version}</version> 
     </dependency> 

     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-validator</artifactId> 
      <version>4.0.2.GA</version> 
     </dependency> 

     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-validator</artifactId> 
      <version>4.0.2.GA</version> 
     </dependency> 
     <!-- Test --> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>4.7</version> 
      <scope>test</scope> 
     </dependency> 
    </dependencies> 

    <repositories> 
     <!-- For testing against latest Spring snapshots --> 
     <repository> 
      <id>org.springframework.maven.snapshot</id> 
      <name>Spring Maven Snapshot Repository</name> 
      <url>http://maven.springframework.org/snapshot</url> 
      <releases> 
       <enabled>false</enabled> 
      </releases> 
      <snapshots> 
       <enabled>true</enabled> 
      </snapshots> 
     </repository> 
     <!-- For developing against latest Spring milestones --> 
     <repository> 
      <id>org.springframework.maven.milestone</id> 
      <name>Spring Maven Milestone Repository</name> 
      <url>http://maven.springframework.org/milestone</url> 
      <snapshots> 
       <enabled>false</enabled> 
      </snapshots> 
     </repository> 
     <!-- For Hibernate Validator --> 
     <repository> 
      <id>org.jboss.repository.releases</id> 
      <name>JBoss Maven Release Repository</name> 
      <url>https://repository.jboss.org/nexus/content/repositories/releases</url> 
      <snapshots> 
       <enabled>false</enabled> 
      </snapshots> 
     </repository> 
     <!-- For Sun Mojarra JSF 2 implementation --> 
     <repository> 
      <id>maven2-repository.dev.java.net</id> 
      <name>Java.net Repository for Maven</name> 
      <url>http://download.java.net/maven/2/</url> 
      <snapshots> 
       <enabled>false</enabled> 
      </snapshots> 
     </repository> 
     <!-- For PrimeFaces JSF component library --> 

    </repositories> 

    <build> 
     <finalName>WebFlowTemplate</finalName> 
    </build> 
</project> 

,這裏是我的database.xml

<?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:context="http://www.springframework.org/schema/context" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xmlns:jdbc="http://www.springframework.org/schema/jdbc" 

    xsi:schemaLocation="http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans.xsd 
          http://www.springframework.org/schema/context 
          http://www.springframework.org/schema/context/spring-context-3.1.xsd 
          http://www.springframework.org/schema/tx 
          http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
          http://www.springframework.org/schema/jdbc 
          http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd"> 

    <context:property-placeholder location="classpath:app.properties" /> 

    <context:component-scan base-package="org.uftwf" /> 

    <tx:annotation-driven transaction-manager="hibernateTransactionManager" /> 

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" 
     destroy-method="close"> 

     <!-- these are C3P0 properties  --> 
     <property name="acquireIncrement" value="${database.c3p0.acquireIncrement}" /> 
     <property name="minPoolSize" value="${database.c3p0.minPoolSize}" /> 
     <property name="maxPoolSize" value="${database.c3p0.maxPoolSize}" /> 
     <property name="maxIdleTime" value="${database.c3p0.maxIdleTime}" /> 
     <property name="maxIdleTimeExcessConnections" value="${database.c3p0.maxIdleTimeExcessConnections}" /> 
     <property name="numHelperThreads" value="${database.c3p0.numHelperThreads}" /> 
     <property name="unreturnedConnectionTimeout" value="${database.c3p0.unreturnedConnectionTimeout}" /> 
     <property name="idleConnectionTestPeriod" value="300" /> 

     <property name="driverClass" value="${database.driver}" /> 
     <property name="jdbcUrl" value="${database.url}" /> 
     <property name="user" value="${database.user}" /> 
     <property name="password" value="${database.password}" /> 
    </bean> 

    <bean id="sessionFactory" 
     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="annotatedClasses"> 
      <list> 
       <value>org.uftwf.memberinquiry.model.MemberInquiryInformation</value> 
      </list> 
     </property> 

     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">${hibernate.dialect}</prop> 
       <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> 
       <prop key="hibernate.use_sql_comments">${hibernate.use_sql_comments}</prop> 
       <prop key="format_sql">${format_sql}</prop> 
       <prop key="hibernate.c3p0.min_size">10</prop> 
       <prop key="hibernate.c3p0.max_size">25</prop> 
       <prop key="hibernate.c3p0.timeout">600</prop> 
       <prop key="hibernate.c3p0.max_statements">0</prop> 
       <prop key="hibernate.c3p0.idle_test_period">300</prop> 
       <prop key="hibernate.c3p0.acquire_increment">5</prop> 
      </props> 
     </property> 
    </bean> 

    <bean id="hibernateTransactionManager" 
     class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="sessionFactory" /> 
    </bean> 
</beans> 

回答

3

嘗試將此添加到您的服務:

@Transactional(readOnly=true, propagation=Propagation.REQUIRED) 
0

看起來像一個懶加載問題。您是否啓用了OpenSessionFactoryInViewFilter或OpenSessionFactoryInViewInterceptor?在事務中執行的任何代碼都不應該得到這個異常,但是渲染視圖通常發生在需要過濾器/攔截器的事務之外。很難說沒有看到代碼。

+0

它應用於versiion 3 –

+0

工作,你想什麼代碼,看看 –

+0

我希望看到一個完整的堆棧跟蹤以及類拋出異常的代碼 – hyness

0
public Session currentSession() throws HibernateException { 
    Object value = TransactionSynchronizationManager.getResource(this.sessionFactory); 
    if (value instanceof Session) { 
     return (Session) value; 
    } 
    else if (value instanceof SessionHolder) { 
     SessionHolder sessionHolder = (SessionHolder) value; 
     Session session = sessionHolder.getSession(); 
     if (TransactionSynchronizationManager.isSynchronizationActive() && 
       !sessionHolder.isSynchronizedWithTransaction()) { 
      TransactionSynchronizationManager.registerSynchronization(
        new SpringSessionSynchronization(sessionHolder, this.sessionFactory)); 
      sessionHolder.setSynchronizedWithTransaction(true); 
      // Switch to FlushMode.AUTO, as we have to assume a thread-bound Session 
      // with FlushMode.MANUAL, which needs to allow flushing within the transaction. 
      FlushMode flushMode = session.getFlushMode(); 
      if (FlushMode.isManualFlushMode(flushMode) && 
        !TransactionSynchronizationManager.isCurrentTransactionReadOnly()) { 
       session.setFlushMode(FlushMode.AUTO); 
       sessionHolder.setPreviousFlushMode(flushMode); 
      } 
     } 
     return session; 
    } 
    else if (this.jtaSessionContext != null) { 
     Session session = this.jtaSessionContext.currentSession(); 
     if (TransactionSynchronizationManager.isSynchronizationActive()) { 
      TransactionSynchronizationManager.registerSynchronization(new SpringFlushSynchronization(session)); 
     } 
     return session; 
    } 
    else { 
     throw new HibernateException("No Session found for current thread"); 
    } 
} 

這是來自SpringSessionContext.java拋出的異常,可以調試代碼檢查笏已經發生了varible價值,我們之前遇到這個問題,因爲我們使用hibernate3.SessionHolder,那麼它不能從instanceof中識別出來,希望這可能會有所幫助。

0
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource"/> 
    <property name="hibernateProperties"> 
     <props> 
       <prop key="hibernate.current_session_context_class">thread</prop> 
     </props> 
    </property> 
</bean> 
相關問題