Hibernate无法获取当前线程的事务同步会话

时间:2014-12-06 22:42:17

标签: java spring hibernate transactions

我遇到了类似的问题,就像许多人一直有的那样,但我似乎无法弄清楚在我的具体案例中出了什么问题。我正在进行一个简单的数据库调用来测试数据库连接,Hibernate抛出了以下异常:

Exception in thread "main" org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:134)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)
at boardwalk.computeServer.dao.DbDaoHibernateImpl.getInterpolationJob(DbDaoHibernateImpl.java:73)
at boardwalk.computeServer.ComputeServer.test(ComputeServer.java:39)
at boardwalk.computeServer.ComputeServer.main(ComputeServer.java:32)

以下是相关代码和配置:

的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/xsd/maven-    4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>boardwalk</groupId>
  <artifactId>computeServer</artifactId>
  <version>1.0</version>
  <packaging>jar</packaging>

  <name>marketserver</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>4.1.1.RELEASE</version>
  </dependency>
  <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.33</version>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>4.1.1.RELEASE</version>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>4.1.1.RELEASE</version>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.1.1.RELEASE</version>
  </dependency>
  <dependency>
    <groupId>javax.transaction</groupId>
    <artifactId>jta</artifactId>
    <version>1.1</version>
  </dependency>
  <dependency>
    <groupId>activesoap</groupId>
    <artifactId>jaxp-api</artifactId>
    <version>1.3</version>
  </dependency>
  <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>4.3.7.Final</version>
  </dependency>
  <dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-all</artifactId>
    <version>5.10.0</version>
  </dependency>
  <dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-spring</artifactId>
    <version>5.10.0</version>
    <type>xsd</type>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jms</artifactId>
    <version>4.1.1.RELEASE</version>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>4.1.1.RELEASE</version>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>4.1.1.RELEASE</version>
  </dependency>
  <dependency>
    <groupId>c3p0</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.1.2</version>
  </dependency>
  <dependency>
    <groupId>org.hibernate.javax.persistence</groupId>
    <artifactId>hibernate-jpa-2.0-api</artifactId>
    <version>1.0.1.Final</version>
  </dependency>
  <dependency>
    <groupId>net.rforge</groupId>
    <artifactId>REngine</artifactId>
    <version>0.6-8.1</version>
  </dependency>
  <dependency>
    <groupId>net.rforge</groupId>
    <artifactId>Rserve</artifactId>
    <version>0.6-8.1</version>
  </dependency>
  <dependency>
    <groupId>org.R</groupId>
    <artifactId>RSession</artifactId>
    <version>1.0</version>
    </dependency>
  </dependencies>
</project>

Hibernate DAO(为了清楚起见,省略了映射对象,因为我认为它们不会导致问题,因为在使用它们之前抛出了异常):

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.dao.DataAccessException;

import boardwalk.computeServer.data.InterpolationDesiredPoint;
import boardwalk.computeServer.data.InterpolationJob;

public class DbDaoHibernateImpl implements DbDao {

    private final SessionFactory sessionFactory;

    public DbDaoHibernateImpl(SessionFactory sessionFactory){
        // Save the session factory
        this.sessionFactory = sessionFactory;
    }


    @Override
    public InterpolationJob getInterpolationJob(int jobId) {

        // Get the session
        Session s = sessionFactory.getCurrentSession();

        // Load the object from the database and return it
        return (InterpolationJob) s.get(InterpolationJob.class, jobId);

    }

}

主要课程:

import org.apache.log4j.Logger;
import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
import org.springframework.transaction.annotation.Transactional;

import boardwalk.computeServer.dao.DbDao;

public class ComputeServer {

    private static final Logger LOG = Logger.getLogger(ComputeServer.class);

    /**
     * Runs the server
     * @param args none
     */
    public static void main(String[] args) {

        // Create the application context
        @SuppressWarnings("resource")
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/beans.xml");
    context.registerShutdownHook();

        // Log the server start
        LOG.info("Server has started");

        test(context);
    }


   @Transactional
   public static void test(ClassPathXmlApplicationContext context){
      DbDao dao = (DbDao) context.getBean("dbDao");
      System.err.println(dao.getInterpolationJob(1));
   }

}

Spring配置文件:

<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
    http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context-4.1.xsd">

<!-- Load the properties file -->
<context:property-placeholder location="classpath:application.properties" />

<!-- C3PO pooled database connections -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close">
    <property name="driverClass" value="${jdbc.driverClassName}" />
    <property name="jdbcUrl" value="${jdbc.url}" />
    <property name="user" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />

    <!-- These are C3P0 properties -->
    <property name="acquireIncrement" value="1" />
    <property name="minPoolSize" value="1" />
    <property name="maxPoolSize" value="10" />
</bean>

<!-- Hibernate session factory -->
<bean id="hibernateSessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="boardwalk.computeServer.data" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${jdbc.dialect}</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

<!-- The database DAO -->
<bean id="dbDao" class="boardwalk.computeServer.dao.DbDaoHibernateImpl">
    <constructor-arg ref="hibernateSessionFactory" />
</bean>

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


<context:annotation-config></context:annotation-config>
</beans>

我应该注意,当我用“openSession()”替换“getCurrentSession()”时,程序运行正常。

提前谢谢!

2 个答案:

答案 0 :(得分:20)

test()方法上的@Transactional是无用的,它只适用于spring托管bean。这就是没有交易背景的原因。

正如@hiimjames所说,在您的DAO类或方法上放置@Transactional

答案 1 :(得分:4)

另一种方法是使用以下注释运行DAO测试:@RunWith(SpringJUnit4ClassRunner.class)

这样 @Transactional 注释就可以了。我会选择避免在DAO级别使用事务。交易应该发生在服务上。