简体   繁体   English

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

[英]Getting an EJBTransactionRolledbackException when trying to connect to a MYSQL database

I'm trying to functionality to login with credentials from the database.我正在尝试使用数据库中的凭据登录的功能。 So far I've just been getting an EJBTransactionRolledbackException.到目前为止,我刚刚收到 EJBTransactionRolledbackException。 The stack trace is huge and so far I have not been able to find anything online related to my specific issue.堆栈跟踪很大,到目前为止,我还没有在网上找到与我的具体问题相关的任何内容。

So the MySQL database I have set up has tables divided up into logical data.因此,我建立的 MySQL 数据库将表划分为逻辑数据。 I have a user_info table with a memberID, addressID, loginID, firstName, lastName, email, phoneNumber, and isModerator.我有一个 user_info 表,其中包含 memberID、addressID、loginID、firstName、lastName、email、phoneNumber 和 isModerator。 My user_login table has loginID, username, and password.我的 user_login 表有 loginID、用户名和密码。 The user_address table is not necessary at this point in the program.程序中此时不需要 user_address 表。 The loginID from the user_login table is a foreign key in the user_info table. user_login 表中的 loginID 是 user_info 表中的外键。 So I essentially do an inner join to get all the info from bot tables and then try to create a new user object and return it.所以我基本上做了一个内部连接来从机器人表中获取所有信息,然后尝试创建一个新用户 object 并返回它。 I've tried just pulling data from one table but the same issue persists.我试过从一张表中提取数据,但同样的问题仍然存在。 The query being used in the Java code works just fine in MySQL workbench. Java 代码中使用的查询在 MySQL 工作台中运行良好。

Here is the method in question, it's the findEntry method:这是有问题的方法,它是 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;

    }

Here is where the findEntry is being called:这是调用 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;

    }

This is the onLogin method in the login controller:这是登录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", user); FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("user", user); //If authentication fails, returns the error page. //如果认证失败,返回错误页面。 if(accountBusinessInterface.validateUser(user) == 0) { //Return the products page. if(accountBusinessInterface.validateUser(user) == 0) { //返回产品页面。 return "ProductsPage.xhtml";返回“ProductsPage.xhtml”;

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

    }

Here is my custom exception:这是我的自定义异常:

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);

    }

}

The stack trace is too long, but here are the first two lines:堆栈跟踪太长,但这里是前两行:

19:11:22,668 ERROR [stderr] (default task-18) Before start of result set 19:11:22,668 错误 [stderr](默认任务 18)在结果集开始之前

19:11:22,671 ERROR [org.jboss.as.ejb3.invocation] (default task-18) WFLYEJB0034: EJB Invocation failed on component UserDataService for method public beans.User data.UserDataService.findEntry(java.lang.String): javax.ejb.EJBTransactionRolledbackException 19:11:22,671 错误 [org.jboss.as.ejb3.invocation] (默认任务 18)WFLYEJB0034:EJB 调用在方法公共 beans.User data.UserDataService.findEntry(java.lang.String) 的组件 UserDataService 上失败: javax.ejb.EJBTransactionRolledbackException

The rest of the stack trace is in a file here since I couldn't paste it here: https://www.dropbox.com/s/r4ampjxr7clfzjz/log.txt?dl=0堆栈跟踪的 rest 位于此处的文件中,因为我无法将其粘贴到此处: https://www.dropbox.com/s/r4ampjxr7clfzjz/log.txt?dl=0

The expected result is that a user will be returned from the findEntry method, which is checked in the validateUser method on the business logic, and if it does not return null then 0 is returned which is checked in the login controller which should log the user in. Obviously something is wring with the database being rolled back.预期的结果是会从 findEntry 方法返回一个用户,该方法在业务逻辑的 validateUser 方法中检查,如果它没有返回 null 则返回 0,在登录 controller 中检查应该记录用户很明显,数据库正在回滚。 I'm just not sure what that means or what is causing it to happen or how to fix it.我只是不确定这意味着什么或导致它发生的原因或如何解决它。 Did I leave any important code or xml files out?我是否遗漏了任何重要的代码或 xml 文件?

You have to move the curser in the result set first, this is what the error message " Before start of result set " is telling you.您必须首先在结果集中移动光标,这就是错误消息“ Before start of result set ”告诉您的内容。

So move the curser first before reading from it.因此,在读取之前先移动光标。 ResultSet#next() will return true if it is not already at the end.如果 ResultSet#next() 还没有结束,它将返回 true。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 试图连接到mysql数据库 - trying to connect to mysql database 尝试将MySQL数据库连接到android时遇到NullPointerException错误 - Getting a NullPointerException error trying to connect a MySQL database to android 尝试通过Eclipse,Java连接到mysql数据库时访问被拒绝 - Access denied when trying to connect to mysql database via eclipse, java 尝试使用 jdbc 连接到 mysql 数据库 - Trying to connect to mysql database with jdbc Azure-尝试连接到外部MySQL数据库时权限被拒绝 - Azure - permission denied when trying to connect to external MySQL database 尝试连接到MySQL时出现NullPointerException - NullPointerException when trying to connect to MySQL 尝试连接到mysql数据库,出现异常提示“必须在以下位置指定端口号: - Trying to connect to a mysql database, getting exception saying Must specify port number after: 尝试连接到MySQL数据库时Java中的NullPointerException - NullPointerException in java while trying to connect to MySQL database 我正在尝试使用休眠xml映射连接到mysql数据库,但出现此错误 - I am trying to connect to a mysql database with hibernate xml mapping but I am getting this error 尝试连接到 android 上的 mongodb 数据库时出错 - Getting an error trying to connect to mongodb database on android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM