On an Exception, How can we access vendorCode and SQLState?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

On an Exception, How can we access vendorCode and SQLState?

vetxtc
I've read the post "mybatis has no exceptions?" (
http://groups.google.com/group/mybatis-user/browse_thread/thread/c3ef7d8e1669d1be/bf33d09d220e94bb?lnk=gst&q=exception+Handling#bf33d09d220e94bb
), as well as, other similar posts but have been unable to find an
answer.

On a MyBatis Exception we want to get the "cause.vendorCode" and the
"cause.SQLState" - which we can see in the Eclipse debugger.   For
example, if an INSERT gets a DUBLICATE KEY error (MySQL Code=1062 ,
SQLState=23000), then we want to UPDATE instead - otherwise it is a
SQL true error and it should be processed as such.

This maybe a dumb question, so apologies in advance, but since the
SQLException is wrapped by MyBatis, how can we access vendorCode and
SQLState?

Thanks!
Reply | Threaded
Open this post in threaded view
|

Re: On an Exception, How can we access vendorCode and SQLState?

Clinton Begin
Administrator
Iterate through getCause() until you find the SQLException.  SQLException itself can contain a cause, so the hard part is knowing if it's the right SQLExcepiton.  Refer to your JDBC driver docs to be sure.

If you're using MyBatis 3, you can trust that the SQLException did not come from the framework (with the exception of PooledDataSource, which is legal because it's a JDBC DataSource implementation).  This was done specifically because SQLExceptions should be the domain of the JDBC driver.  

So with MyBatis 3, it's pretty simple. 

Unfortunately if you're using MyBatis 2.x, it broke that paradigm, and throws SQLExceptions for some things.  

The good news is that if you know well enough the codes you're looking for, then it should be pretty easy to determine which SQLEXception is the right one, even with MyBatis 2.x.  

Clinton

On Mon, Sep 20, 2010 at 6:10 PM, vetxtc <[hidden email]> wrote:
I've read the post "mybatis has no exceptions?" (
http://groups.google.com/group/mybatis-user/browse_thread/thread/c3ef7d8e1669d1be/bf33d09d220e94bb?lnk=gst&q=exception+Handling#bf33d09d220e94bb
), as well as, other similar posts but have been unable to find an
answer.

On a MyBatis Exception we want to get the "cause.vendorCode" and the
"cause.SQLState" - which we can see in the Eclipse debugger.   For
example, if an INSERT gets a DUBLICATE KEY error (MySQL Code=1062 ,
SQLState=23000), then we want to UPDATE instead - otherwise it is a
SQL true error and it should be processed as such.

This maybe a dumb question, so apologies in advance, but since the
SQLException is wrapped by MyBatis, how can we access vendorCode and
SQLState?

Thanks!

Reply | Threaded
Open this post in threaded view
|

Re: On an Exception, How can we access vendorCode and SQLState?

vetxtc
Clinton, thanks for the help!  I am using MyBatis 3 and have been a
big fan of i/My-Batis since 1.0 so thanks for all your efforts.  They
are appreciated!

Can you elaborate or provide a code snippet on your comment "iterate
through getCause() until you find the SQLException"?

There is one solution on this wiki which grabs the cause error message
and parses through looking for the error code.  I'd prefer to grab the
error code from the cause but do not see a method to do so.

FYI, the wiki link to the parse solution is:
http://groups.google.com/group/mybatis-user/browse_thread/thread/d975e7fd1c65f86b/ca19487c2c6c0763?lnk=gst&q=getCause#ca19487c2c6c0763

Thanks,
-Bill

On Sep 20, 8:54 pm, Clinton Begin <[hidden email]> wrote:

> Iterate through getCause() until you find the SQLException.  SQLException
> itself can contain a cause, so the hard part is knowing if it's the right
> SQLExcepiton.  Refer to your JDBC driver docs to be sure.
>
> If you're using MyBatis 3, you can trust that the SQLException did not come
> from the framework (with the exception of PooledDataSource, which is legal
> because it's a JDBC DataSource implementation).  This was done specifically
> because SQLExceptions should be the domain of the JDBC driver.
>
> So with MyBatis 3, it's pretty simple.
>
> Unfortunately if you're using MyBatis 2.x, it broke that paradigm, and
> throws SQLExceptions for some things.
>
> The good news is that if you know well enough the codes you're looking for,
> then it should be pretty easy to determine which SQLEXception is the right
> one, even with MyBatis 2.x.
>
> Clinton
>
> On Mon, Sep 20, 2010 at 6:10 PM, vetxtc <[hidden email]> wrote:
> > I've read the post "mybatis has no exceptions?" (
>
> >http://groups.google.com/group/mybatis-user/browse_thread/thread/c3ef...
> > ), as well as, other similar posts but have been unable to find an
> > answer.
>
> > On a MyBatis Exception we want to get the "cause.vendorCode" and the
> > "cause.SQLState" - which we can see in the Eclipse debugger.   For
> > example, if an INSERT gets a DUBLICATE KEY error (MySQL Code=1062 ,
> > SQLState=23000), then we want to UPDATE instead - otherwise it is a
> > SQL true error and it should be processed as such.
>
> > This maybe a dumb question, so apologies in advance, but since the
> > SQLException is wrapped by MyBatis, how can we access vendorCode and
> > SQLState?
>
> > Thanks!
Reply | Threaded
Open this post in threaded view
|

Re: On an Exception, How can we access vendorCode and SQLState?

Clinton Begin
Administrator
getCause() is a standard method on the Throwable interface.  So you can do something like:

//coding in gmail, so gimme a break.  :-)
public SQLException findSQLExceptionWithVendorErrorCode (Throwable t) {
  Throwable rootCause = t;
  // this can become an endless loop if someone circularly nests an exception, which would be dumb, but throw a control in there anyway
  while (rootCause != null) {
    if (rootCause instanceof SQLException) {
        SQLException sqlException = (SQLException) rootCause;
        if (sqlException.getVendorCode() == SOME_EXPECTED_VALUE)  { // or not equal to some unexpected value, like 0
           return sqlException;
        }
    }
    rootCause = t.getCause();
  }
  return null;  // is there a better way to indicate that no SQLException was found?
}


On Mon, Sep 20, 2010 at 10:18 PM, vetxtc <[hidden email]> wrote:
Clinton, thanks for the help!  I am using MyBatis 3 and have been a
big fan of i/My-Batis since 1.0 so thanks for all your efforts.  They
are appreciated!

Can you elaborate or provide a code snippet on your comment "iterate
through getCause() until you find the SQLException"?

There is one solution on this wiki which grabs the cause error message
and parses through looking for the error code.  I'd prefer to grab the
error code from the cause but do not see a method to do so.

FYI, the wiki link to the parse solution is:
http://groups.google.com/group/mybatis-user/browse_thread/thread/d975e7fd1c65f86b/ca19487c2c6c0763?lnk=gst&q=getCause#ca19487c2c6c0763

Thanks,
-Bill

On Sep 20, 8:54 pm, Clinton Begin <[hidden email]> wrote:
> Iterate through getCause() until you find the SQLException.  SQLException
> itself can contain a cause, so the hard part is knowing if it's the right
> SQLExcepiton.  Refer to your JDBC driver docs to be sure.
>
> If you're using MyBatis 3, you can trust that the SQLException did not come
> from the framework (with the exception of PooledDataSource, which is legal
> because it's a JDBC DataSource implementation).  This was done specifically
> because SQLExceptions should be the domain of the JDBC driver.
>
> So with MyBatis 3, it's pretty simple.
>
> Unfortunately if you're using MyBatis 2.x, it broke that paradigm, and
> throws SQLExceptions for some things.
>
> The good news is that if you know well enough the codes you're looking for,
> then it should be pretty easy to determine which SQLEXception is the right
> one, even with MyBatis 2.x.
>
> Clinton
>
> On Mon, Sep 20, 2010 at 6:10 PM, vetxtc <[hidden email]> wrote:
> > I've read the post "mybatis has no exceptions?" (
>
> >http://groups.google.com/group/mybatis-user/browse_thread/thread/c3ef...
> > ), as well as, other similar posts but have been unable to find an
> > answer.
>
> > On a MyBatis Exception we want to get the "cause.vendorCode" and the
> > "cause.SQLState" - which we can see in the Eclipse debugger.   For
> > example, if an INSERT gets a DUBLICATE KEY error (MySQL Code=1062 ,
> > SQLState=23000), then we want to UPDATE instead - otherwise it is a
> > SQL true error and it should be processed as such.
>
> > This maybe a dumb question, so apologies in advance, but since the
> > SQLException is wrapped by MyBatis, how can we access vendorCode and
> > SQLState?
>
> > Thanks!