mercredi 13 mai 2015

Java Jdbc Transaction

I have a requirement to execute sequence of select,update,insert queries in jdbc.

here is my code:

 public String editRequest(){
connection = DatabaseUtil.getServiceConnection();
            try {
                connection.setAutoCommit(false);
                executeUpdateCategory(category, ticketid);
                executeUpdateSubCategory(subcategory,
                        ticketid);
                executeUpdateItem(item, ticketid));
                executeUpdateImpact(impact),ticketid));
                  connection.commit();
                 response = "Success";
            } catch (SQLException e) {
                rollback();
            }
    return response;
    }

Method Definitions:

    public void executeCategory(category,ticketid){
        try{
        //select query using connection and prepared statement

        }
        catch(SQLException e){
        e.printstacktrace();
        }

etc... methods

Note:Since it is a single transaction.So if we get any exception inside the methods it is executing some of the statements.it is Calling commit() method.

But these methods are reusable.Could any one how to catch the Exception inside editRequest method catch block so that i can rollback the transaction if any error?

Aucun commentaire:

Enregistrer un commentaire