Spring事务上下文不持久保存数据

2023-12-22

我知道我的问题是一个常见问题,但是我在这里检查了很多问题,检查了Spring文档,我真的不知道我做错了什么。 我的问题:我有一个使用 JPA 的 Spring WebFlow 项目(实现:OpenJPA + MySQL 数据库)。我使用 Spring ORM 将 EntityManager(通过 @PersistenceContext 注释)注入到我的简单 RegisterDAO 中。我已经配置了 GlassFishs(我正在使用的)连接池来使用 MySQL,并且一切正常 - 我可以使用我的数据库,但是当我持久化某些内容时 - 什么也没有发生(数据没有持久化到数据库)。我知道问题出在我使用的事务上下文上。我阅读了 Spring Transaction Management 的文档并按照该文档中的配置步骤进行操作。这是我的 applicationContext.xml:

<?xml version="1.0" encoding="windows-1250"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
       http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">

    <jee:jndi-lookup id="entityManagerFactory" jndi-name="myPersistenceUnit"/> 
    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

    <bean id="registerDaoImpl" class="umk.dumont.db.dao.RegisterDAO" />
    <bean id="registerModel" class="umk.dumont.models.RegisterFormModel">
        <property name="registerDAO" ref="registerDaoImpl" />
    </bean>


  <tx:advice id="txAdvice">
    <tx:attributes>
      <tx:method name="*" />
    </tx:attributes>
  </tx:advice> 

  <aop:config>
    <aop:pointcut id="registerModelOperation" expression="execution(* umk.dumont.models.RegisterFormModel.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="registerModelOperation"/>
  </aop:config>

  <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager" />


</beans>

正如您所看到的,我将 RegisterDAO 注入到 RegisterFormModel 中,其中包含用于验证注册表单数据并最终将用户添加到数据库的业务逻辑。验证工作正常,当我尝试添加新用户时出现问题。这是代码:

package umk.dumont.models;

...

public class RegisterFormModel implements Serializable {
    private String login;
    private String password;
    private String email;
    @Autowired
    private RegisterDAO registerDAO = null;

...

public boolean addUser()
    {
        MyUser user = new MyUser();
        user.setLogin(login);
        user.setPassword(password);
        user.setEmail(email);
        return registerDAO.insertUserIntoDB(user) == 0 ? true : false;
    }

...
}

注册DAO:

public class RegisterDAO implements RegisterDAOInterface, Serializable {
    private EntityManager em;

    @PersistenceContext
    public void setEm(EntityManager em)
    {
        this.em = em;
    }


...
public int insertUserIntoDB(MyUser user)
    {
        int result = -4;
        try {
            em.persist(user);
            result = 0;

        }
        catch(Exception ex)
        {
            ex.printStackTrace();
            result = -4;
        }
        finally {

            return result;
        }
    }
...
}

我也尝试过使用@Transactional 注释。我像这样配置了 spring applicationContext.xml:

<?xml version="1.0" encoding="windows-1250"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
       http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">



    <jee:jndi-lookup id="entityManagerFactory" jndi-name="myPersistenceUnit"/> 
    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
    <bean id="registerDaoImpl" class="umk.dumont.db.dao.RegisterDAO" />
    <bean id="registerModel" class="umk.dumont.models.RegisterFormModel">
        <property name="registerDAO" ref="registerDaoImpl" />
    </bean>


  <tx:annotation-driven />
  <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager" />


</beans>

并使用 @Transactional 注释来注释我的 addUser() 方法,如下所示:

package umk.dumont.models;

...

public class RegisterFormModel implements Serializable {
    private String login;
    private String password;
    private String email;
    @Autowired
    private RegisterDAO registerDAO = null;

...
@Transactional
public boolean addUser()
    {
        MyUser user = new MyUser();
        user.setLogin(login);
        user.setPassword(password);
        user.setEmail(email);
        return registerDAO.insertUserIntoDB(user) == 0 ? true : false;
    }

...
}

甚至用这个注释来注释整个类:

package umk.dumont.models;

...
@Transactional    
public class RegisterFormModel implements Serializable {
    private String login;
    private String password;
    private String email;
    @Autowired
    private RegisterDAO registerDAO = null;

...
public boolean addUser()
    {
        MyUser user = new MyUser();
        user.setLogin(login);
        user.setPassword(password);
        user.setEmail(email);
        return registerDAO.insertUserIntoDB(user) == 0 ? true : false;
    }

...
}

但在这两种情况下,问题是相同的 - 数据未存储在数据库中。我的 AOP 代理有什么问题吗,因为我是这方面的新手(就像整个 Spring 一样:))?

编辑:在我的 persistence.xml 中我使用transaction-type="JTA“所以我想我应该使用<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager" />在我的 applicationContext.xml 中 - 我是对的吗?


使用解决了我的问题

org.springframework.orm.jpa.JpaTransactionManager

所以你的豆子应该是

 <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" />

希望这个工作!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring事务上下文不持久保存数据 的相关文章

随机推荐