UserTransaction不提交

问题描述 投票:0回答:1

我正在尝试使用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) {
                    }

                }

            }
        }
jsf jpa transactions managed-bean
1个回答
1
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.