[英]Getting an EJBTransactionRolledbackException when trying to connect to a MYSQL database
我正在嘗試使用數據庫中的憑據登錄的功能。 到目前為止,我剛剛收到 EJBTransactionRolledbackException。 堆棧跟蹤很大,到目前為止,我還沒有在網上找到與我的具體問題相關的任何內容。
因此,我建立的 MySQL 數據庫將表划分為邏輯數據。 我有一個 user_info 表,其中包含 memberID、addressID、loginID、firstName、lastName、email、phoneNumber 和 isModerator。 我的 user_login 表有 loginID、用戶名和密碼。 程序中此時不需要 user_address 表。 user_login 表中的 loginID 是 user_info 表中的外鍵。 所以我基本上做了一個內部連接來從機器人表中獲取所有信息,然后嘗試創建一個新用戶 object 並返回它。 我試過從一張表中提取數據,但同樣的問題仍然存在。 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", user); //如果認證失敗,返回錯誤頁面。 if(accountBusinessInterface.validateUser(user) == 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.invocation] (默認任務 18)WFLYEJB0034:EJB 調用在方法公共 beans.User data.UserDataService.findEntry(java.lang.String) 的組件 UserDataService 上失敗: javax.ejb.EJBTransactionRolledbackException
堆棧跟蹤的 rest 位於此處的文件中,因為我無法將其粘貼到此處: 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")...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.