UserTransaction没有提交

时间:2015-01-07 16:03:43

标签: jsf jpa transactions managed-bean

我正在尝试使用JSF应用程序重置密码,但userTransaction不起作用。我将所有正确的值分配给实体,然后我执行utx.commit()但在数据库中没有任何更改。显示“密码已重置”消息,不会发生错误 你能帮助我吗?这是代码:

package it.polimi.meteocal.business.control;

import it.polimi.meteocal.business.beans.SendEmailBean;
import it.polimi.meteocal.business.entity.User;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.bean.ManagedBean;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;

@ManagedBean
@RequestScoped
public class AccessValidation {

    private String password;
    private String confpassword;
    private String username4email;
    private String message;
    private String email;
    private String username;

    @PersistenceContext
    EntityManager em;

    @EJB
    private CheckFields cf;

    @Resource
    UserTransaction utx;

    public void passwordReset() {

            Query query;
            User user;

            if (!cf.checkPassword(password, confpassword)) {
                message = "Passwords don't match";
            } else if (confpassword.length() < 6) {
                message = "Password should be at least 6 characters";
            } else {

                try {

                    query = em.createQuery("select u from User u where u.username=:um");
                    query.setParameter("um", username);
                    user = (User) query.getResultList().get(0);

                    utx.begin();

                    user.setPassword(password);

                    utx.commit();
                    message = "Password has been reset!";

                } catch (Exception e) {

                    e.printStackTrace();
                    try {
                        utx.rollback();
                    } catch (IllegalStateException | SecurityException | SystemException exception) {
                    }

                }

            }
        }

1 个答案:

答案 0 :(得分:1)

query.getResultList()之后,用户与实体管理器分离。这是因为实体管理器上下文从utx.begin()utx.commit()存在。没有活动事务的任何查询结果都不会包含在实体管理器上下文中。

解决方案:将utx.begin()放在查询之前。