【问题标题】:Is JTA manager necessary to use transaction features in hibernate 4在休眠 4 中使用事务功能是否需要 JTA 管理器
【发布时间】:2013-08-08 20:28:59
【问题描述】:

我在这里学习教程: http://www.javacodegeeks.com/2013/05/hibernate-4-with-spring.html 在我的 Java Web 应用程序中启用“@Transactional 注释”但未能使其正常运行。请告知是否真的需要 JTA 经理,为什么?

请注意,我的 webapp 基于 Spring 3 + Hibernate 4 + Tomcat 7。

背景和我的疑惑:

我当前的 Web 应用程序使用我自己的自定义类(实现 HandlerInterceptor)来启用每个请求一个休眠会话。现在我想改用“@Transactional 注释”来提高我的应用程序的可维护性,因为这样可以节省很多代码行。

根据我的理解,@Transactional 基本上是依靠AOP的概念来保​​证会话(Hibernate session)在注解的方法中准备好使用的。这似乎与 JTA 无关。但我想知道为什么我不能让它在 Tomcat 7 中的 webapp 上运行(没有 JTA-provider)。

在 google 上搜索了几次后,似乎需要 JTA。这让我感到困惑,因为这似乎是一个非常基本的功能,不应该将复杂的 JTA 提供程序作为要求。

这是我得到的错误:

org.hibernate.HibernateException: No Session found for current thread
    org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
    org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:988)
    ...

这是我用于测试的代码:

....

@Autowired
org.hibernate.SessionFactory sessionFactory;

@Transactional
@RequestMapping(method = RequestMethod.GET)
protected String home() {
    Session session = sessionFactory.getCurrentSession(); // I expected the session is good to use now
    Province p = (Province) session.get(Province.class, 1L); // This causes no session found error :(


    return "home";
}

春季 XML:

....
<tx:annotation-driven/>
<context:component-scan base-package="..."/>

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/jdbc/..."/>
    <property name="lookupOnStartup" value="true"/>
    <property name="proxyInterface" value="javax.sql.DataSource"/>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.hbm2ddl.auto">update</prop>

            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
        </props>
    </property>
</bean>

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

<bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
....

谢谢!

【问题讨论】:

  • 教程链接无效
  • 我想知道为什么它也被删除了:(。实际上该链接在我发布时是有效的。它仍然存在于我的浏览器历史记录中。

标签: spring hibernate tomcat7


【解决方案1】:

只是猜测:

您的控制器是在某种 dispatcher-servlet.xml 中定义的,因此与定义 的 applicationContext 是分开的。如果我没记错的话,您想使用 @Transactional 增强的组件需要与 在同一上下文中。所以@Transactional 不起作用。

【讨论】:

  • 感谢您的帮助。看到您的评论后,我只是尝试将它们移到同一个地方,但这没有帮助。 :(
  • 我想我刚刚明白你为什么要指出这一点。我只使用 org.springframework.web.servlet.DispatcherServlet 作为我在 web.xml 中的唯一上下文。还是谢谢。
【解决方案2】:

那是我的愚蠢错误。 Spring使用CGLIB来代理带有@Transactional注释的方法,似乎CBLIB无法增强受保护的方法。

protected String home() {

改成

public String home() {

解决了问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-09
    • 1970-01-01
    • 1970-01-01
    • 2014-07-05
    • 2011-12-10
    • 1970-01-01
    相关资源
    最近更新 更多