【问题标题】:spring, hibernate and declarative transaction implementation: there is no active transactionspring、hibernate和声明式事务实现:没有活动事务
【发布时间】:2010-11-10 05:50:24
【问题描述】:

我尝试使声明式事务工作。

这是我的 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: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>

这是我的控制器实现:

//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();
    }

}

两者都在名为 test 的包内。

这是我的尝试:

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

但这会引发异常:

org.hibernate.HibernateException: createQuery 在没有活动事务的情况下无效

为什么 transactionManager 不起作用?我希望使用 @Transactional 注释所有事务都将由 Spring 框架管理。 我能做什么?

谢谢大家。

【问题讨论】:

    标签: hibernate spring transactions annotations


    【解决方案1】:

    去掉以下几行,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事务管理,见AbstractSessionFactoryBean.setExposeTransactionAwareSessionFactory() javadoc

    关闭此标志以暴露平原 休眠 SessionFactory 与 Hibernate 的默认设置 getCurrentSession() 行为, 支持纯 JTA 同步 只要。或者,只需覆盖 对应的 Hibernate 属性 “hibernate.current_session_context_class”。

    【讨论】:

    • 宾果游戏!谢谢!我的目标是广泛使用注释(@Autowired,@Transactional ecc),以减少写入 xml 文件的配置量。我朝着正确的方向前进?我必须改变一些东西或做一些优化?
    • @blow:是的,你正朝着正确的方向前进。请注意,如果您使用 Spring 3 并且真的不喜欢 XML 配置,则可以通过使用 @Configuration 配置它们来减少 XML 中的基础架构组件(例如 transactionManagersessionFactory 等)的数量。
    • 是否有任何文档说明删除上述密钥以启用 Spring 管理事务?
    【解决方案2】:

    我似乎记得,当类实现这样的接口时,您可能会使用带有原型注释的类(如 @Controller)得到奇怪的行为。

    我不确定 100% 的解决方法是什么,但请尝试以下一种或两种方法:

    • @TransactionalTestControllerImp.findAll() 移动到TestController.findAll()
    • proxy-target-class="true" 添加到您的&lt;tx:annotation-driven/&gt;

    一个或两个都可以解决问题,但都不是理想的。我以前在其他问题中看到过这种情况,但从未完全了解导致它的原因。

    【讨论】:

    • 感谢您的回复,我都尝试了, 并在接口级别移动@Transactional,但问题仍然存在。跨度>
    猜你喜欢
    • 2014-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多