Results 1 to 3 of 3
- 05-02-2011, 07:09 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
How to handle commit errors in JPA
I have a question about JPA/JTA error handling. If I commit a transaction, and it fails, what is the proper way to bring my entities back under management, fix the problem, and then re-commit?
Specifically, I am wondering how to determine the exact error that occured (so I can present it to the uer for correction). Also, it is my understanding that all entities become unmanaged after commit failure, so how should I handle that?
Also, are their vendor-specific API's for help with error handling that I should be using?
Thanks for any and all advice. This is a very confusing area to me.
Eric
- 05-06-2011, 07:44 PM #2
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
I'm still hoping to get some advice as to the best way to handle this. Anyone?
Eric
- 05-07-2011, 05:31 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 64
- Rep Power
- 0
i'm new to java but have worked with several db's before. Depending on what DBMS you're using you should be able to use transactions. MsSQL and MySQL both support this and Access does not. I think it's better to use transactions as this lets the database system take care of checking for errors, and fixing them as opposed to have your code do it. so, the transaction does the insert/query and if it's successful, it moves to the next query/insert. If all are successful, you can then commit their changes. if they are not successful, you can roll back the changes. theirs lots out there if you google database transactions and commit/rollbacks. as for how to detect specific errors, on the code side, i use this:
hope that helps a littleJava Code:try{ //your code here } catch (SQLException e) { while (e != null) { // Retrieve a human-readable message identifying the reason for the exception String message = e.getMessage(); String sqlState = e.getSQLState(); int errorCode = e.getErrorCode(); String driverName = myConnection.getMetaData().getDriverName(); if (driverName.equals("JDBC-ODBC Bridge (ACEODBC.DLL)") && errorCode == 0 ) { // Process error... //this code looks for sql inserts into a unique column but you can look for any error based on its returned error code JOptionPane.showMessageDialog(this, "I'm sorry, you already have a district by this name, please choose a unique District Name.");\ } else{ } // The exception may have been chained; process the next chained exception e = e.getNextException(); }
Similar Threads
-
First Java Program-Compile Errors (errors are posted)-simple GUI
By cc11rocks in forum AWT / SwingReplies: 4Last Post: 01-04-2011, 12:36 AM -
EntityManager.setFlushMode(COMMIT) Vs. Query.setFlushMode(COMMIT)
By CatchSandeepVaid in forum Enterprise JavaBeans (EJB)Replies: 0Last Post: 12-15-2009, 05:07 PM -
commit
By swati.jyoti in forum NetBeansReplies: 1Last Post: 07-24-2009, 05:33 PM -
2 phase commit using JBosss JTA
By amt_asc in forum JDBCReplies: 1Last Post: 04-03-2009, 02:25 PM -
Commit problem
By egc_33 in forum EclipseReplies: 0Last Post: 08-09-2008, 02:27 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks