2012-11-28 74 views
1

我試圖用Spring應用程序上下文連接Hibernate。我successfulyl連接最多一個SessionFactory對象,但是當我嘗試調用getCurrentSession我看到下列錯誤:Hibernate sessionFactory.getCurrentSession()當與Spring一起使用時

org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here 
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean$TransactionAwareInvocationHandler.invoke(AbstractSessionFactoryBean.java:270) 
at $Proxy8.getCurrentSession(Unknown Source) 
at org.company.web.ws.sample.impl.CheckSecurityImpl.verify(CheckSecurityImpl.java:31) 
at org.company.web.ws.sample.impl.CheckSecurityImplTest.testCheckSecurity(CheckSecurityImplTest.java:28) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
at java.lang.reflect.Method.invoke(Method.java:597) 
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) 
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) 
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74) 
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83) 
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) 
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) 
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:236) 
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174) 
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 

我配置數據源會話工廠和事務管理。我還指定了交易處理將被標註驅動

這裏是我的上下文的xml:

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
    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.0.xsd 
    http://www.springframework.org/schema/jdbc 
    http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
    "> 


<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 
    <property name="driverClassName" value="org.hsqldb.jdbcDriver" /> 
    <property name="url" value="jdbc:hsqldb:mem:murex" /> 
    <property name="username" value="sa" /> 
    <property name="password" value="" /> 
</bean> 

<jdbc:initialize-database data-source="dataSource"> 
    <jdbc:script location="classpath:/db-schema.sql"/> 
    <jdbc:script location="classpath:/db-testdata.sql"/> 
</jdbc:initialize-database> 

<!-- Hibernate Session Factory --> 
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="mappingResources"> 
     <list> 
      <value>/security.hbm.xml</value> 
     </list> 
    </property> 
    <property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop> 
      <prop key="hibernate.hbm2ddl.auto">update</prop> 
      <prop key="hibernate.show_sql">true</prop> 
     </props> 
    </property> 
</bean> 

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory"/> 
</bean> 

<tx:annotation-driven/> 

<bean id="checkSecurityService" class="org.example.web.ws.sample.impl.CheckSecurityImpl" /> 

這裏就是我seeign錯誤的類:

@WebService(endpointInterface = "org.example.web.ws.sample.CheckSecurity") 
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true) 
public class CheckSecurityImpl implements CheckSecurity { 

    @Autowired 
    private SessionFactory sessionFactory; 

    @Transactional 
    public CheckSecurityResponse verify(String propertyValue, String propertyType) { 
     Session session = sessionFactory.getCurrentSession(); 
    } 
} 

當這被稱爲sessionFactory不爲null,但錯誤與上述錯誤。如果我使用openSession,一切正常,但我知道這不適用於事務。

任何想法?謝謝

+0

休眠3或4? – ElderMael

+0

http://stackoverflow.com/questions/4961636/no-hibernate-session-bound-to-thread-and-configuration-does-not-allow-creation,可能有幫助 – invariant

+0

什麼是處理您的自動裝配?我在context.xml中看不到組件掃描或註釋配置。這是在不同的背景下? – MarkOfHall

回答

2

上下文中缺少組件掃描,因此未運行@Transactional後處理器來裝飾事務封裝的方法。

-1

你可以發佈完整的堆棧跟蹤嗎?

我猜這個例外是TransactionManager而不是getCurrentSession()verify()

您正在分別使用TransactionManagerSession

您可以通過刪除交易的註釋來快速測試。

要正確修復,請嘗試使用HibernateTemplate而不是直接使用SessionFactory

+0

嗨sgp,感謝您的迴應,我添加了完整的堆棧跟蹤到問題。在添加交易註釋之前,我有同樣的錯誤! – cowls

+0

另外,HibernateTemplate是推薦的方法嗎?這種方式讓我感覺更清潔,因爲我沒有被束縛到春天。 – cowls

+1

不建議使用HibernateTemplate。請參閱http://static.springsource.org/autorepo/docs/spring/3.0.x/api/org/springframework/orm/hibernate3/HibernateTemplate.html:「注意:從Hibernate 3.0.1開始,事務Hibernate訪問代碼可以也可以用簡單的Hibernate風格編碼,因此,對於新開始的項目,考慮採用標準的Hibernate3風格的編碼數據訪問對象,而不是基於SessionFactory.getCurrentSession()。「 – mmalmeida

-1

使用以下罐子

<dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-core</artifactId> 
     <version>4.0.1.Final</version> 
    </dependency> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-core</artifactId> 
     <version>4.0.1.Final</version> 
    </dependency> 
    <dependency> 
     <groupId>org.hibernate.common</groupId> 
     <artifactId>hibernate-commons-annotations</artifactId> 
      <version>4.0.1.Final</version> 
    </dependency> 

    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-entitymanager</artifactId> 
     <version>4.0.1.Final</version> 
    </dependency> 

    <dependency> 
     <groupId>antlr</groupId> 
     <artifactId>antlr</artifactId> 
     <version>2.7.7</version> 
    </dependency> 
    <dependency> 
<groupId>org.antlr</groupId> 
<artifactId>antlr-runtime</artifactId> 
<version>3.0</version> 

相關問題