I am using a Callable Statement to run a stored procedure in the database.
The stored procedure takes an input parameter and passes an output parameter
of type int. The output parameter is used to perform a test to see if the
stored procedure worked properly, or passed some logic tests on the database
side. If the output parameter is 0, then I commit, otherwise I must perform
a rollback.
Now, the problem is, everything looks like it's running perfectly. I get no
SQL exceptions, my output parameter is 0, so I call Connection.commit() and
Connection.setAutoCommit(true). However, the database doesn't update, it's
as if the commit statement never gets run, but no exception is thrown.
Note: If I run this in autocommit mode it runs fine, but I am not able to
perform an explicit rollback when I get an incorrect output parameter, so my
data will change when it should NOT change.
Here is the code:
con.setAutoCommit(false);
CallableStatement cs = con.prepareCall("{call mysp (?,
?)}");
cs.registerOutParameter(2, java.sql.Types.INTEGER);
cs.setInt(1, 12);
cs.execute();
int myresult=cs.getInt(2);
System.out.println("Update done for: 12");
System.out.println("Result=" + myresult);
if (myresult!=0) {
System.out.println("Update failed!");
try {con.rollback();
con.setAutoCommit(true);
} catch (SQLException xcp)
{System.out.println("Exception: " + xcp.getMessage();}
} else {con.commit();
con.setAutoCommit(true);
}
Output when I test:
Update done for: 12
Result=0
The entire thing is surrounded with a SQLException handler etc, but this is
the core of the code which performs the function.
The only odd thing about the stored procedure, is that it uses a temp table
to do some data manipulation before it updates the data that I am affecting.
Any suggestions would be appreciated.
Thanks!
Kathy
Joe Weinstein - 05 Dec 2003 16:01 GMT
> I am using a Callable Statement to run a stored procedure in the database.
> The stored procedure takes an input parameter and passes an output parameter
[quoted text clipped - 10 lines]
> perform an explicit rollback when I get an incorrect output parameter, so my
> data will change when it should NOT change.
Hi. Interesting! What DBMS are you using, and what jdbc driver?
Off hand, this seems all OK. Can you show us the text of the procedure?
Joe Weinstein at BEA
> Here is the code:
>
[quoted text clipped - 31 lines]
> Thanks!
> Kathy
Tim Jowers - 16 Dec 2003 19:15 GMT
> > I am using a Callable Statement to run a stored procedure in the database.
> > The stored procedure takes an input parameter and passes an output parameter
[quoted text clipped - 50 lines]
> > Thanks!
> > Kathy
Kathy,
I ran into this too... SQL Server (and one would infer Sybase) does
not always throw SQLException or even set SQLWarnings. To no avail I
even tried the TSQL "RaisError" in the stored proc. Well, it seems
that way until you call getMoreResults()... something about the
exception being in the stream from the db... that's a bugger with
using CallableStatement but after adding the getMoreResults after
Execute then life is good once again. (Until the next time :-0)
TimJowers