无法修复"无法获取当前线程的事务同步会话"

时间:2017-08-20 07:25:24

标签: spring hibernate

我是春天休眠的新手。 我使用的是sprin 4.3.8和Hibernate 5.2。

我尝试了网上提供的解决方案,并且我的所有代码都在我的代码中,但是我收到此错误请帮我解决此问题。

错误:

Exception in thread "main" org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
at org.springframework.orm.hibernate5.SpringSessionContext.currentSession(SpringSessionContext.java:133)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:456)
at com.travello.daoImpl.ActivityDAOImpl.getActivity(ActivityDAOImpl.java:43)
at com.travello.model.SpringMain.main(SpringMain.java:17)

这是ActivityListDAOImpl:

@Transactional
public class ActivityDAOImpl implements ActivityDAO {

@Autowired
private SessionFactory sessionfactory;



public SessionFactory getSessionfactory() {
    return sessionfactory;
}

public void setSessionfactory(SessionFactory sessionfactory) {
    this.sessionfactory = sessionfactory;
}

@Override
public ActivityList getActivity(int activity_id) {
    Session session = sessionfactory.getCurrentSession();
    ActivityList activity = session.get(ActivityList.class, activity_id);
    return activity;
    }
}

主要方法:

public class SpringMain {


public static void main(String[] args) {

    @SuppressWarnings("resource")
    ApplicationContext context = new ClassPathXmlApplicationContext         ("spring.xml");
    ActivityDAOImpl dao = (ActivityDAOImpl) context.getBean("activityDao", ActivityDAOImpl.class);

    ActivityList activity = dao.getActivity(1);
    System.out.println(activity.getActivityName());
    System.out.println("Done");


}

}

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

    <context:property-placeholder location="classpath:resources/database.properties" />
    <context:annotation-config/>
    <context:component-scan base-package="com.travello" />

<bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${database.driver}" />
        <property name="url" value="${database.url}" />
        <property name="username" value="${database.user}" />
        <property name="password" value="${database.password}" />
    </bean>


    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="hibernate.cfg.xml" />
    </bean>



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



    <bean id="activityDao" class="com.travello.daoImpl.ActivityDAOImpl">
        <property name="sessionfactory" ref="sessionFactory" />
    </bean>

    </beans>

我在spring.xml中应用了@Transational注释和相同的bean,但是出现了这个错误。

2 个答案:

答案 0 :(得分:0)

<tx:annotation-driven/>添加到spring.xml。请参阅herehere

答案 1 :(得分:-1)

我有一个类似的错误,最简单的解决方案是添加@Transaction与    @Transactional(propagation = Propagation.REQUIRES_NEW)

此后您将不会收到此错误,并且可以正常工作。