java - 尝试连接到 MYSQL 数据库时出现 EJBTransactionRolledbackException

标签 java mysql jboss ejb

我正在尝试使用数据库中的凭据登录。到目前为止,我刚刚收到 EJBTransactionRolledbackException。堆栈跟踪非常巨大,到目前为止我还无法在网上找到与我的具体问题相关的任何内容。

因此,我设置的 MySQL 数据库将表划分为逻辑数据。我有一个 user_info 表,其中包含 memberID、addressID、loginID、firstName、lastName、email、phoneNumber 和 isModerator。我的 user_login 表包含登录 ID、用户名和密码。此时程序中不需要 user_address 表。 user_login 表中的loginID 是user_info 表中的外键。因此,我本质上执行内部联接以从机器人表中获取所有信息,然后尝试创建一个新的用户对象并返回它。我尝试过从一张表中提取数据,但同样的问题仍然存在。 Java 代码中使用的查询在 MySQL 工作台中运行得很好。

这是有问题的方法,它是 findEntry 方法:

@Stateless
@Local(DataAccessInterface.class)
@LocalBean
public class UserDataService implements DataAccessInterface<User> {

    public UserDataService() {



    }

    @Override
    public List<User> findAll() {

        return null;

    }

    @Override
    public User findEntry(String condition) {

        String query = "SELECT * FROM user_info INNER JOIN user_login WHERE username='" + condition + "';";

        Connection databaseConnection = null;

        Statement statement = null;

        ResultSet resultSet = null;

        User currentUser = null;

        try {

            databaseConnection = DriverManager.getConnection(url, username, password);

            statement = databaseConnection.createStatement();

            resultSet = statement.executeQuery(query);

            currentUser = new User(resultSet.getInt("memberID"), resultSet.getInt("addressID"), resultSet.getInt("loginID"), resultSet.getString("firstName"), resultSet.getString("lastName"), resultSet.getString("email"), resultSet.getString("phoneNumber"), resultSet.getString("username"), resultSet.getString("password"), resultSet.getInt("isModerator"));

        }

        catch(SQLException e) {

            throw new DatabaseException(e);

        }

        finally {

            try {

                if(databaseConnection != null) {

                    databaseConnection.close();

                    statement.close();

                    resultSet.close();  

                }


            }

            catch(SQLException e) {

                throw new DatabaseException(e);

            }

        }

        return currentUser;

    }

这里是调用 findEntry 的地方:

@Stateless
@Local(AccountBusinessInterface.class)
@LocalBean
public class AccountBusiness implements AccountBusinessInterface {

    @EJB
    private DataAccessInterface<User> userDataService;

    public AccountBusiness() {



    }

    /**
     * Validates that the use who entered in their username and password entered the correct information.
     */
    @Override
    public int validateUser(User user) {
        //Sets the login boolean to true.
        //user.setLoggedIn(true);
        //Sets the login text to logout.
        //user.setLoginText("Logout");

        User currentUser = userDataService.findEntry(user.getUsername());

        if(currentUser !=  null) {

            return 0;

        }

        return 1;

    }

这是登录 Controller 中的 onLogin 方法:

@ManagedBean
@ViewScoped
public class LoginController {

    /**
     * This is the BusinessAccountInferface.
     */
    @Inject
    private AccountBusinessInterface accountBusinessInterface;

    /**
     * The default constructor.
     */
    public LoginController() {



    }

    /**
     * Takes in a user object and returns the product page that can only be seen by a logged in user, assuming the correct
     * username and password was entered.
     * @param user
     * @return String
     */
    public String onLogin(User user) {
        //Gets the user object from the appropriate form.

FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("用户", user); //如果认证失败,则返回错误页面。 if(accountBusinessInterface.validateUser(用户) == 0) { //返回产品页面。 返回“ProductsPage.xhtml”;

        }
        //Returns the login page by default.
        return "Login.xhtml";

    }

这是我的自定义异常:

public class DatabaseException extends RuntimeException {

    /**
     * This is the default serial version id.
     */
    private static final long serialVersionUID = 1L;

    public DatabaseException() {

        printStackTrace();

    }

    public DatabaseException(SQLException e) {

        printMessage(e.getMessage());

    }

    public DatabaseException(String message) {

        printMessage(message);

    }

    public DatabaseException(SQLException e, String message) {

        printMessage(e.getMessage());

        printMessage(message);

    }

    private void printMessage(String message) {

        System.err.println(message);

    }

}

堆栈跟踪太长,但这里是前两行:

19:11:22,668 错误 [stderr](默认任务 18)在结果集开始之前

19:11:22,671 错误 [org.jboss.as.ejb3.inspiration](默认任务 18)WFLYEJB0034:方法 public beans.User data.UserDataService.findEntry(java.lang.字符串):javax.ejb.EJBTransactionRolledbackException

堆栈跟踪的其余部分位于此处的一个文件中,因为我无法将其粘贴到此处: https://www.dropbox.com/s/r4ampjxr7clfzjz/log.txt?dl=0

预期的结果是findEntry方法返回一个用户,该用户在业务逻辑的validateUser方法中进行检查,如果不返回null则返回0,在登录 Controller 中进行检查,这应该是让用户登录。显然数据库回滚出了问题。我只是不确定这意味着什么或是什么导致它发生或如何解决它。我是否遗漏了任何重要的代码或 xml 文件?

最佳答案

您必须首先在结果集中移动光标,这就是错误消息“Before start of result set”告诉您的内容。

因此,在读取之前先移动光标。如果尚未结束,ResultSet#next() 将返回 true。

if (resultSet.next()){
    currentUser = new User(resultSet.getInt("memberID")...
}

关于java - 尝试连接到 MYSQL 数据库时出现 EJBTransactionRolledbackException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58674538/

相关文章:

java - 为什么将客户端 JBoss 连接回收到远程队列后仍然抛出 SpyJMSExceptions?

Java/Android 自动解析泛型类型。寻求逐步指导以了解

java - 公共(public)到本地 IP 通信

mysql - myBatis 有条件插入非空值

mysql - 根据记录的条目查找用户列表

java - JBOSS 7 - Spring ContextLoaderListener ClassNotFoundException

java - 如何修复: Unable to invoke no-args constructor for class X: Registering an InstanceCreator with Gson for this type may fix this problem

java-如何计算段落之间的空格并从总行数中减去它们?

mysql - 导入 MySQL 数据库

eclipse - 你如何集成 gradle 和 jboss 6 来生成工作的 eclipse 项目