Results 1 to 3 of 3
  1. #1
    mn1247 is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default 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

  2. #2
    mn1247 is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    I'm still hoping to get some advice as to the best way to handle this. Anyone?

    Eric

  3. #3
    jasonwucinski is offline Member
    Join Date
    Feb 2011
    Posts
    64
    Rep Power
    0

    Default

    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:

    Java 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();
    
                   
                     
            }
    hope that helps a little

Similar Threads

  1. Replies: 4
    Last Post: 01-04-2011, 01:36 AM
  2. EntityManager.setFlushMode(COMMIT) Vs. Query.setFlushMode(COMMIT)
    By CatchSandeepVaid in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 12-15-2009, 06:07 PM
  3. commit
    By swati.jyoti in forum NetBeans
    Replies: 1
    Last Post: 07-24-2009, 06:33 PM
  4. 2 phase commit using JBosss JTA
    By amt_asc in forum JDBC
    Replies: 1
    Last Post: 04-03-2009, 03:25 PM
  5. Commit problem
    By egc_33 in forum Eclipse
    Replies: 0
    Last Post: 08-09-2008, 03:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •