Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Callable or PreparedStatement and transactions

I have a prepared statement call in java that calls a procedure that commits or rolls back a sybase transaction. Is there anyway I can check from my java calls the result of that transaction, ie. success for commit or failure for rollback? Alternatively, I want to check that the procedure completed without errors - whats the best way of doing that? Thanks

like image 708
horli34 Avatar asked Jul 23 '10 16:07

horli34


2 Answers

If the rollback or commit happens within the stored procedure, then the only way to know the success is to have the stored procedure return its success status. Then, Java will have access to this result.

like image 192
Erick Robertson Avatar answered Oct 19 '22 11:10

Erick Robertson


A stored procedure should return some kind of status or error code.
Put that in an OUT parameter and read that from the proc.

     //Calling a stored procedure which takes in 2 parameters for addition
     /*
     --EXECUTE ADDITION 10,25,NULL
     ALTER PROCEDURE ADDITION
     @A INT
     , @B INT
     , @C INT OUT
     AS
     SELECT @C = @A + @B
     */
     CallableStatement cs2 = con.prepareCall("{call ADDITION(?,?,?)}");
     cs2.registerOutParameter(3,java.sql.Types.INTEGER);
     cs2.setInt(1,10);
     cs2.setInt(2,25);
     cs2.execute();
     int res = cs2.getInt(3);
     System.out.println(res);
like image 2
Romain Hippeau Avatar answered Oct 19 '22 12:10

Romain Hippeau