views:

63

answers:

3

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

A: 

I think you're using the wrong approach. The client (the JAVA code in this case) should be committing or rolling back, not the sybase stored procedure. I assume here that by "calling a procedure" you mean a stored procedure that commits or rolls back.

If you use the approach I just mentioned, then you can put everything in a try/catch block and handle the commit/rollback accordingly.

try {
    PreparedStatement ps = create prepared statement;
    ps.execute();

    // nothing went wrong, commit
} catch (SqlException e) {
    // something wen't wrong, rollback
}

If by "calling a procedure" you mean just another JAVA method, then the I don't know why you would have a separate method to do a commit or rollback, you can handle it all in the try/catch block as shown above.

dcp
It's very common in the big-iron world for stored procedures to manage their own transactions, particularly when those procedures are called from a variety of (badly written) clients. it may not be the java way, but it's by no means the "wrong way".
skaffman
Good point, thanks for the comment. I guess it depends on whether the stored procedure is acting as the "client", in which case I would agree with you. But if the JAVA code is handling the business operations (insert into tablex, delete from tabley, all as 1 unit of work, etc.), I stand by my original answer, namely, the client controls the transaction logic. You can refer to another answer I gave (http://stackoverflow.com/questions/3317390/which-should-take-initiative-task-on-rollback-app-or-db/3317421#3317421) and I agree 100% with Tom Kyte.
dcp
A: 

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.

Erick Robertson
The 'transaction' occurs within the stored procedure so I would use its value to return to the java code. Thanks for your help.
horli34
+1  A: 

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);
Romain Hippeau