本文介绍了春天,休眠和声明式事务管理实现:不存在活跃交易的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我努力使声明式事务的工作。

i try to make declarative transaction work.

这是我的spring.xml文件:

This is my spring.xml file:

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

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

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="url" value="jdbc:h2:tcp://my/db/path" />
        <property name="username" value="username" />
        <property name="password" value="password" />
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="data" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
                <prop key="hibernate.current_session_context_class">thread</prop>
                <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>

    <context:component-scan base-package="test" />

    <tx:annotation-driven/>

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

</beans>

这是我的控制器实现:

And this is my controller implementation:

//file TestController.java
public interface TestController {

    public List<Test> findAll();

}

//file TestControllerImp.java
@Controller
public class TestControllerImp implements TestController{

    @Autowired
    private SessionFactory sessionFactory;

    /**
     * @return the sessionFactory
     */
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    /**
     * @param sessionFactory the sessionFactory to set
     */
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory=sessionFactory;
    }

    @Transactional
    public List<Test> findAll() {
        return sessionFactory.getCurrentSession().createQuery("from Test").list();
    }

}

两者都在里面的包叫做测试。

Both are inside package called test.

这是我的尝试:

TestController tc=context.getBean(TestController.class);
List<Test> list=tc.findAll();

但是,这引发异常:

But this throw an exception:

org.hibernate.HibernateException:的createQuery是无效的无活动事务

为什么transactionManager在不工作?我希望与@Transactional注释的所有交易将被Spring框架进行管理。
我能做些什么?

Why transactionManager doesn't work? I hope with @Transactional annotation all transactions will be managed by spring framework.What can i do?

谢谢大家。

推荐答案

删除以下行,当交易由Spring管理是不需要的:

Remove the following lines, they are not needed when transactions are managed by Spring:

<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>

其实,设置的hibernate.current_session_context_class 有效地禁止Spring的事务管理,看<$c$c>AbstractSessionFactoryBean.setExposeTransactionAwareSessionFactory() javadoc的:

Actually, setting hibernate.current_session_context_class effectively disables Spring transaction management, see AbstractSessionFactoryBean.setExposeTransactionAwareSessionFactory() javadoc:

关闭这个标志,露出平原
  Hibernate的SessionFactory与
  Hibernate的默认
  的getCurrentSession()的行为,
  支持普通的JTA同步
  只要。或者,干脆重写
  相应的Hibernate属性
  的hibernate.current_session_context_class。

这篇关于春天,休眠和声明式事务管理实现:不存在活跃交易的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 21:15