2

I have a java program that is calling a MySQL stored procedure that is rolled back when it gets an SQLEXCEPTION. When I added the rollback (exit handler) to the stored procedure the Java program stopped getting the SQL exception.

How can I make sure the SQL exception and MySQL error message are propagated back to the Java program?

Here is my store procedure:

DELIMITER $$

DROP PROCEDURE IF EXISTS up_OMS_insertParticipantOmsOrderOwner $$
CREATE PROCEDURE up_OMS_insertParticipantOmsOrderOwner(
                 IN PID int,
                 IN OwnerName varchar(50),
                 IN DisplayName varchar(50),
                 IN Enabled tinyint(1))

BEGIN
declare exit handler for SQLException
  BEGIN
    rollback;
  END;
start transaction;

if (DisplayName<>'') then
  insert OmsOrderOwner (ParticipantID, OmsOrderOwnerName, DisplayName, Enabled)
  value (PID, OwnerName,DisplayName, Enabled);
else
  insert OmsOrderOwner(ParticipantID, OmsOrderOwnerName, DisplayName, Enabled)
  value (PID, OwnerName,null, Enabled);
end if;

set @OwnerID := @@identity;

insert UserOmsOrderOwnerSubscription (UserID, ParticipantID, OmsOrderOwnerID, Enabled)
select
  userOrderSub.UserId, PID, @OwnerID, 1
from
  Users u,
  UserOmsOrderSubscription userOrderSub
where
  userOrderSub.UserID = u.UserID and
  u.ParticipantID = PID;

commit;

END $$

DELIMITER ;

2 Answers 2

3

Use RESIGNAL statement in your exit handler to rethrow the error.

That said, do you REALLY need to explicitly begin / commit / rollback transaction within your stored procedure? JDBC call will be (should be) done within its own transaction anyway, can you instead rely on it to handle the error / rollback and save yourself some trouble, perhaps?

Sign up to request clarification or add additional context in comments.

1 Comment

yeah we may move the commit and rollback in our java code. unfortunately we can't do that in the short term. Also unfortunate is that we are on version 5.0x of mysql so resignal doesn't work. I can write a signalFunct myself but i won't get the mysql error message.
-1

Since you handled the error in STP, it's not an exception anymore. It should be just a normal return status of your call. You should return something from the exit handler, like

declare exit handler for SQLException
  BEGIN
    rollback;
    select 1;
  END;
start transaction;

1 or whatever will be error code for rollback.

If you still think this is an exception, you can use resignal in MySQL 6.0. In earlier version , you can just trigger an error by calling a non-existant function like this,

 call ROLLED_BACK_EXCEPTION();

1 Comment

Resignal works starting with 5.4, not 6.0. And creating a fake error (by calling non-existent function) instead of returning an actual one is less than ideal for many reasons.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.