我知道我的问题是一个常见问题,但我在这里检查了很多问题,检查了Spring文档,我真的不知道我在做什么错误。 我的问题:我有一个使用JPA的Spring WebFlow项目(实现:OpenJPA + MySQL数据库)。我使用Spring ORM向我简单的RegisterDAO注入EntityManager(通过@PersistenceContext注释)。我配置了GlassFishs(我正在使用)使用MySQL的连接池,一切正常 - 我可以使用我的数据库,但是当我坚持一些东西 - 没有任何反应(数据不会持续到数据库)。我知道问题出在我使用的事务上下文上。我阅读了Spring Transaction Management的文档,并按照本文档中的配置步骤进行操作。这是我的applicationContext.xml:Spring事务上下文不会持久数据
<?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;
}
...
}
RegisterDAO:
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注解。我配置的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>
并注明我ADDUSER()方法@Transactional注释是这样的:
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
“,所以我觉得我应该在我的applicationContext.xml使用<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager" />
- 我说的对
是的 - 这看起来像JTA的正确的bean定义。 – 2011-03-11 07:16:19