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 Bhaarat Sharma <bh...@gmail.com> on 2009/05/11 17:26:58 UTC

error handling in iBatis?

we have been using iBatis for a while now.
However, we do not perform any error handling while executing the sql maps.
 Back when we used java code to connect, we used try and catch blocks.
 Where the catch block would log an error message.

something like this:

            try {
                Conn = getConnection(DataCollection.HEREMS);
                cstmt = Conn.prepareCall(storProc);
                cstmt.setString(1, user.toUpperCase());
                rs = cstmt.executeQuery();
                while (rs.next()) {
                    String roleId = rs.getString("role_code");
                    Roles.add(roleId);
                }
            }catch (SQLException ex) {
                log.error(ex.getMessage(),ex);
            }finally {
               try {
                  releaseConnection(Conn, cstmt, null, rs);
               }catch (SQLException ex) {
                log.error(ex.getMessage(),ex);
               }
            }

Now with sqlMaps we simply do something like this:

        getSqlMapClientTemplate().queryForList("debtowed.getSingleTenant",
paramMap);

        if (paramMap.size() == 3)
        {
            fmr = (FormerTenants) paramMap.get("single_tenant_results");

 fmr.setTerminationReason(paramMap.get("single_tenant_rejection_results"));
        }

        return fmr;

In the above code we are expecting 3 resultsets so all we are doing is
checking if map size is 3.

What if some error happens while executing the stored procedure.  Where will
that error go?  Is there a way in iBatis to log error messages? I would
appreciate some sample which would point me in the right direction.

Thanks

Re: error handling in iBatis?

Posted by Sundar Sankar <fa...@gmail.com>.
I guess you can use the same kinda code to catch exception. I batis does
throw SQLException for errors. If you are using Spring, then it is wrapped
to something else (DAOException, I think). You could throw back custom
exception or a new SQLException when the size doesnt match ur need.

I would have written Unit test for the same and tested out the erroneous
cases to make sure of what I was getting!

Let us know if it didnt work

-Sundar

On Mon, May 11, 2009 at 8:26 AM, Bhaarat Sharma <bh...@gmail.com> wrote:

> we have been using iBatis for a while now.
> However, we do not perform any error handling while executing the sql maps.
>  Back when we used java code to connect, we used try and catch blocks.
>  Where the catch block would log an error message.
>
> something like this:
>
>             try {
>                 Conn = getConnection(DataCollection.HEREMS);
>                 cstmt = Conn.prepareCall(storProc);
>                 cstmt.setString(1, user.toUpperCase());
>                 rs = cstmt.executeQuery();
>                 while (rs.next()) {
>                     String roleId = rs.getString("role_code");
>                     Roles.add(roleId);
>                 }
>             }catch (SQLException ex) {
>                 log.error(ex.getMessage(),ex);
>             }finally {
>                try {
>                   releaseConnection(Conn, cstmt, null, rs);
>                }catch (SQLException ex) {
>                 log.error(ex.getMessage(),ex);
>                }
>             }
>
> Now with sqlMaps we simply do something like this:
>
>         getSqlMapClientTemplate().queryForList("debtowed.getSingleTenant",
> paramMap);
>
>         if (paramMap.size() == 3)
>         {
>             fmr = (FormerTenants) paramMap.get("single_tenant_results");
>
>  fmr.setTerminationReason(paramMap.get("single_tenant_rejection_results"));
>         }
>
>         return fmr;
>
> In the above code we are expecting 3 resultsets so all we are doing is
> checking if map size is 3.
>
> What if some error happens while executing the stored procedure.  Where
> will that error go?  Is there a way in iBatis to log error messages? I would
> appreciate some sample which would point me in the right direction.
>
> Thanks
>

Re: error handling in iBatis?

Posted by Nathan Maves <na...@gmail.com>.
You can see from the java docs that each method does infact throw a
SqlException.
http://ibatis.apache.org/docs/java/user/com/ibatis/sqlmap/client/SqlMapExecutor.html

You need to be prepared to catch them if you care to do anything with them.

Nathan

On Mon, May 11, 2009 at 9:26 AM, Bhaarat Sharma <bh...@gmail.com> wrote:

> we have been using iBatis for a while now.
> However, we do not perform any error handling while executing the sql maps.
>  Back when we used java code to connect, we used try and catch blocks.
>  Where the catch block would log an error message.
>
> something like this:
>
>             try {
>                 Conn = getConnection(DataCollection.HEREMS);
>                 cstmt = Conn.prepareCall(storProc);
>                 cstmt.setString(1, user.toUpperCase());
>                 rs = cstmt.executeQuery();
>                 while (rs.next()) {
>                     String roleId = rs.getString("role_code");
>                     Roles.add(roleId);
>                 }
>             }catch (SQLException ex) {
>                 log.error(ex.getMessage(),ex);
>             }finally {
>                try {
>                   releaseConnection(Conn, cstmt, null, rs);
>                }catch (SQLException ex) {
>                 log.error(ex.getMessage(),ex);
>                }
>             }
>
> Now with sqlMaps we simply do something like this:
>
>         getSqlMapClientTemplate().queryForList("debtowed.getSingleTenant",
> paramMap);
>
>         if (paramMap.size() == 3)
>         {
>             fmr = (FormerTenants) paramMap.get("single_tenant_results");
>
>  fmr.setTerminationReason(paramMap.get("single_tenant_rejection_results"));
>         }
>
>         return fmr;
>
> In the above code we are expecting 3 resultsets so all we are doing is
> checking if map size is 3.
>
> What if some error happens while executing the stored procedure.  Where
> will that error go?  Is there a way in iBatis to log error messages? I would
> appreciate some sample which would point me in the right direction.
>
> Thanks
>

RE: error handling in iBatis?

Posted by Winarto <wi...@fermat.eu>.
Hi,

 

I believe you're using Spring-DAO with IBatis as ORM. The method
getSqlMapClientTemplate().queryForList is actually throwing
DataAccessException. However since it is inheritance of
RuntimeException, you do not need to explicitly catch it. But yes, you
can catch it if you want.

 

 

Cheers,

Winarto

 

From: Bhaarat Sharma [mailto:bhaarat.s@gmail.com] 
Sent: Monday, 11 May 2009 23:27
To: user-java@ibatis.apache.org
Subject: error handling in iBatis?

 

we have been using iBatis for a while now.  

 

However, we do not perform any error handling while executing the sql
maps.  Back when we used java code to connect, we used try and catch
blocks.  Where the catch block would log an error message. 

 

something like this:

 

            try {

                Conn = getConnection(DataCollection.HEREMS);

                cstmt = Conn.prepareCall(storProc);

                cstmt.setString(1, user.toUpperCase());

                rs = cstmt.executeQuery();

                while (rs.next()) {

                    String roleId = rs.getString("role_code");

                    Roles.add(roleId);

                }

            }catch (SQLException ex) {

                log.error(ex.getMessage(),ex);

            }finally {

               try {

                  releaseConnection(Conn, cstmt, null, rs);

               }catch (SQLException ex) {

                log.error(ex.getMessage(),ex);

               }

            }

 

Now with sqlMaps we simply do something like this:

 

 
getSqlMapClientTemplate().queryForList("debtowed.getSingleTenant",
paramMap);

 

        if (paramMap.size() == 3)

        {

            fmr = (FormerTenants) paramMap.get("single_tenant_results");

 
fmr.setTerminationReason(paramMap.get("single_tenant_rejection_results")
);

        }

 

        return fmr;

 

In the above code we are expecting 3 resultsets so all we are doing is
checking if map size is 3.

 

What if some error happens while executing the stored procedure.  Where
will that error go?  Is there a way in iBatis to log error messages? I
would appreciate some sample which would point me in the right
direction. 

 

Thanks