You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by 陈抒 <cs...@gmail.com> on 2010/03/28 11:13:28 UTC

Question about how to use rollback

Hello:
  My transactionManager type is JDBC. There is a sample in IBatis3 document:

SqlSession session = sqlSessionFactory.openSession();
try {
    // following 3 lines pseudocod for “doing some work”
    session.insert(...);
    session.update(...);
    session.delete(...);
    session.commit();
} finally {
    session.close();
}

  If  an insert statement failed caused by duplicated key,can I catch
SQLException and call session.rollback()?Like so:
SqlSession session = sqlSessionFactory.openSession();
try {
    // following 3 lines pseudocod for “doing some work”
    session.insert(...);
    session.update(...);
    session.delete(...);
    session.commit();
}catch(SQLException ex){
    session.rollback();
} finally {
    session.close();
}
  I don't want to use any frameworks.


陈抒
Best regards
http://blog.csdn.net/sheismylife

Re: Question about how to use rollback

Posted by Clinton Begin <cl...@gmail.com>.
You don't even have to really... it will be called automatically if you
don't call commit in most cases.  Write a unit test for yourself to confirm
this in your environment.

But yes, what you wrote looks fine.  My only recommendation would be to roll
back regardless of the exception type (e.g. catch Throwable)

Cheers,
Clinton

On Sun, Mar 28, 2010 at 3:13 AM, 陈抒 <cs...@gmail.com> wrote:

> Hello:
>   My transactionManager type is JDBC. There is a sample in IBatis3
> document:
>
> SqlSession session = sqlSessionFactory.openSession();
> try {
>     // following 3 lines pseudocod for “doing some work”
>     session.insert(...);
>     session.update(...);
>     session.delete(...);
>     session.commit();
> } finally {
>     session.close();
> }
>
>   If  an insert statement failed caused by duplicated key,can I catch
> SQLException and call session.rollback()?Like so:
> SqlSession session = sqlSessionFactory.openSession();
> try {
>     // following 3 lines pseudocod for “doing some work”
>     session.insert(...);
>     session.update(...);
>     session.delete(...);
>     session.commit();
> }catch(SQLException ex){
>     session.rollback();
>  } finally {
>     session.close();
> }
>   I don't want to use any frameworks.
>
>
> 陈抒
> Best regards
> http://blog.csdn.net/sheismylife
>