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 Lieven De Keyzer <li...@hotmail.com> on 2005/05/24 20:27:58 UTC

transactions

At  http://www.reumann.net/struts/ibatisLesson1/step6.do
this is an example in a ibatis/struts tutorial

public int update(String statementName, Object parameterObject) throws 
DaoException {
    int result = 0;
    try {
        sqlMap.startTransaction();
        result = sqlMap.executeUpdate(statementName, parameterObject);
        sqlMap.commitTransaction();
    } catch (SQLException e) {
        try {
            sqlMap.rollbackTransaction();
        } catch (SQLException ex) {
            throw new DaoException(ex.fillInStackTrace());
        }
        throw new DaoException(e.fillInStackTrace());
    }
    return result;
}

Is it necessary to have a transaction started for just 1 statement 
execution?

Also, what's the better way? Doing a transaction in a Service class, that 
has multiple DAO's, or doing it in the DAO class, doing different statements 
in one method? Or is there no difference?



Re: transactions

Posted by Brandon Goodin <br...@gmail.com>.
In this case i would have pulled the individual calls out to the
service layer and wrapped them in a transaction. I have some rare
cases where i have needed to place more complex code in my dao. In
those cases i still wrap the single call to my method in a transaction
on the service layer.

Brandon

On 5/24/05, Brandon Goodin <br...@gmail.com> wrote:
> Message was sent to me privately... so i am posting it to the list
> -----
> But for example, in the JPetStoreExample, in the AccountSqlMapDao, this is a
> method:
> 
>   public void insertAccount(Account account) {
>     update("insertAccount", account);
>     update("insertProfile", account);
>     update("insertSignon", account);
>   }
> 
> Aren't those different update statements better off in a transaction? And
> why no different calls from the Service layer?
> I'm just trying to understand the difference.
> 
> >From: Brandon Goodin <br...@gmail.com>
> >Reply-To: Brandon Goodin <br...@gmail.com>
> >To: ibatis-user-java@incubator.apache.org
> >Subject: Re: transactions
> >Date: Tue, 24 May 2005 12:45:59 -0600
> >
> >It is not neccessary to call transactions on only one statement.
> >
> >Transactions should be handled on the Service layer and make more
> >fine-grained calls to the DAO layer.
> >
> >Brandon
> >
> >On 5/24/05, Lieven De Keyzer <li...@hotmail.com> wrote:
> > > At  http://www.reumann.net/struts/ibatisLesson1/step6.do
> > > this is an example in a ibatis/struts tutorial
> > >
> > > public int update(String statementName, Object parameterObject) throws
> > > DaoException {
> > >     int result = 0;
> > >     try {
> > >         sqlMap.startTransaction();
> > >         result = sqlMap.executeUpdate(statementName, parameterObject);
> > >         sqlMap.commitTransaction();
> > >     } catch (SQLException e) {
> > >         try {
> > >             sqlMap.rollbackTransaction();
> > >         } catch (SQLException ex) {
> > >             throw new DaoException(ex.fillInStackTrace());
> > >         }
> > >         throw new DaoException(e.fillInStackTrace());
> > >     }
> > >     return result;
> > > }
> > >
> > > Is it necessary to have a transaction started for just 1 statement
> > > execution?
> > >
> > > Also, what's the better way? Doing a transaction in a Service class,
> >that
> > > has multiple DAO's, or doing it in the DAO class, doing different
> >statements
> > > in one method? Or is there no difference?
> > >
> > >
> > >
>

Re: transactions

Posted by Lieven De Keyzer <li...@hotmail.com>.

>From: Clinton Begin <cl...@gmail.com>
>Reply-To: cbegin@ibatis.com
>To: ibatis-user-java@incubator.apache.org, Brandon Goodin 
><br...@gmail.com>
>Subject: Re: transactions
>Date: Tue, 24 May 2005 13:40:04 -0600
>
>Those update statements ARE in a transaction. But you should NEVER manage
>transactions inside the DAO. The transaction in this case is taken care of
>outside of the scope of this particular method (I believe in this case it 
>is
>actually an automatic DAO transaction, so you might not find the calls to
>start/commit/end).

So, what you mean is: I should make the transaction in my service layer, and 
inside this transaction call the dao method. But if I change a method in my 
DAO, to have multiple statements, I would also need to change my Service 
class, to place the call in a transaction?
And how does one specify those automatic transactions?
>
>Clinton
>
>On 5/24/05, Brandon Goodin <br...@gmail.com> wrote:
> >
> > Message was sent to me privately... so i am posting it to the list
> > -----
> > But for example, in the JPetStoreExample, in the AccountSqlMapDao, this 
>is
> > a
> > method:
> >
> > public void insertAccount(Account account) {
> > update("insertAccount", account);
> > update("insertProfile", account);
> > update("insertSignon", account);
> > }
> >
> > Aren't those different update statements better off in a transaction? 
>And
> > why no different calls from the Service layer?
> > I'm just trying to understand the difference.
> >
> > >From: Brandon Goodin <br...@gmail.com>
> > >Reply-To: Brandon Goodin <br...@gmail.com>
> > >To: ibatis-user-java@incubator.apache.org
> > >Subject: Re: transactions
> > >Date: Tue, 24 May 2005 12:45:59 -0600
> > >
> > >It is not neccessary to call transactions on only one statement.
> > >
> > >Transactions should be handled on the Service layer and make more
> > >fine-grained calls to the DAO layer.
> > >
> > >Brandon
> > >
> > >On 5/24/05, Lieven De Keyzer <li...@hotmail.com> wrote:
> > > > At http://www.reumann.net/struts/ibatisLesson1/step6.do
> > > > this is an example in a ibatis/struts tutorial
> > > >
> > > > public int update(String statementName, Object parameterObject) 
>throws
> > > > DaoException {
> > > > int result = 0;
> > > > try {
> > > > sqlMap.startTransaction();
> > > > result = sqlMap.executeUpdate(statementName, parameterObject);
> > > > sqlMap.commitTransaction();
> > > > } catch (SQLException e) {
> > > > try {
> > > > sqlMap.rollbackTransaction();
> > > > } catch (SQLException ex) {
> > > > throw new DaoException(ex.fillInStackTrace());
> > > > }
> > > > throw new DaoException(e.fillInStackTrace());
> > > > }
> > > > return result;
> > > > }
> > > >
> > > > Is it necessary to have a transaction started for just 1 statement
> > > > execution?
> > > >
> > > > Also, what's the better way? Doing a transaction in a Service class,
> > >that
> > > > has multiple DAO's, or doing it in the DAO class, doing different
> > >statements
> > > > in one method? Or is there no difference?
> > > >
> > > >
> > > >
> >



Re: transactions

Posted by Clinton Begin <cl...@gmail.com>.
Those update statements ARE in a transaction. But you should NEVER manage 
transactions inside the DAO. The transaction in this case is taken care of 
outside of the scope of this particular method (I believe in this case it is 
actually an automatic DAO transaction, so you might not find the calls to 
start/commit/end). 

Clinton

On 5/24/05, Brandon Goodin <br...@gmail.com> wrote:
> 
> Message was sent to me privately... so i am posting it to the list
> -----
> But for example, in the JPetStoreExample, in the AccountSqlMapDao, this is 
> a
> method:
> 
> public void insertAccount(Account account) {
> update("insertAccount", account);
> update("insertProfile", account);
> update("insertSignon", account);
> }
> 
> Aren't those different update statements better off in a transaction? And
> why no different calls from the Service layer?
> I'm just trying to understand the difference.
> 
> >From: Brandon Goodin <br...@gmail.com>
> >Reply-To: Brandon Goodin <br...@gmail.com>
> >To: ibatis-user-java@incubator.apache.org
> >Subject: Re: transactions
> >Date: Tue, 24 May 2005 12:45:59 -0600
> >
> >It is not neccessary to call transactions on only one statement.
> >
> >Transactions should be handled on the Service layer and make more
> >fine-grained calls to the DAO layer.
> >
> >Brandon
> >
> >On 5/24/05, Lieven De Keyzer <li...@hotmail.com> wrote:
> > > At http://www.reumann.net/struts/ibatisLesson1/step6.do
> > > this is an example in a ibatis/struts tutorial
> > >
> > > public int update(String statementName, Object parameterObject) throws
> > > DaoException {
> > > int result = 0;
> > > try {
> > > sqlMap.startTransaction();
> > > result = sqlMap.executeUpdate(statementName, parameterObject);
> > > sqlMap.commitTransaction();
> > > } catch (SQLException e) {
> > > try {
> > > sqlMap.rollbackTransaction();
> > > } catch (SQLException ex) {
> > > throw new DaoException(ex.fillInStackTrace());
> > > }
> > > throw new DaoException(e.fillInStackTrace());
> > > }
> > > return result;
> > > }
> > >
> > > Is it necessary to have a transaction started for just 1 statement
> > > execution?
> > >
> > > Also, what's the better way? Doing a transaction in a Service class,
> >that
> > > has multiple DAO's, or doing it in the DAO class, doing different
> >statements
> > > in one method? Or is there no difference?
> > >
> > >
> > >
>

transactions

Posted by Brandon Goodin <br...@gmail.com>.
Message was sent to me privately... so i am posting it to the list
-----
But for example, in the JPetStoreExample, in the AccountSqlMapDao, this is a
method:

  public void insertAccount(Account account) {
    update("insertAccount", account);
    update("insertProfile", account);
    update("insertSignon", account);
  }

Aren't those different update statements better off in a transaction? And
why no different calls from the Service layer?
I'm just trying to understand the difference.

>From: Brandon Goodin <br...@gmail.com>
>Reply-To: Brandon Goodin <br...@gmail.com>
>To: ibatis-user-java@incubator.apache.org
>Subject: Re: transactions
>Date: Tue, 24 May 2005 12:45:59 -0600
>
>It is not neccessary to call transactions on only one statement.
>
>Transactions should be handled on the Service layer and make more
>fine-grained calls to the DAO layer.
>
>Brandon
>
>On 5/24/05, Lieven De Keyzer <li...@hotmail.com> wrote:
> > At  http://www.reumann.net/struts/ibatisLesson1/step6.do
> > this is an example in a ibatis/struts tutorial
> >
> > public int update(String statementName, Object parameterObject) throws
> > DaoException {
> >     int result = 0;
> >     try {
> >         sqlMap.startTransaction();
> >         result = sqlMap.executeUpdate(statementName, parameterObject);
> >         sqlMap.commitTransaction();
> >     } catch (SQLException e) {
> >         try {
> >             sqlMap.rollbackTransaction();
> >         } catch (SQLException ex) {
> >             throw new DaoException(ex.fillInStackTrace());
> >         }
> >         throw new DaoException(e.fillInStackTrace());
> >     }
> >     return result;
> > }
> >
> > Is it necessary to have a transaction started for just 1 statement
> > execution?
> >
> > Also, what's the better way? Doing a transaction in a Service class,
>that
> > has multiple DAO's, or doing it in the DAO class, doing different
>statements
> > in one method? Or is there no difference?
> >
> >
> >

Re: transactions

Posted by Brandon Goodin <br...@gmail.com>.
It is not neccessary to call transactions on only one statement.

Transactions should be handled on the Service layer and make more
fine-grained calls to the DAO layer.

Brandon

On 5/24/05, Lieven De Keyzer <li...@hotmail.com> wrote:
> At  http://www.reumann.net/struts/ibatisLesson1/step6.do
> this is an example in a ibatis/struts tutorial
> 
> public int update(String statementName, Object parameterObject) throws
> DaoException {
>     int result = 0;
>     try {
>         sqlMap.startTransaction();
>         result = sqlMap.executeUpdate(statementName, parameterObject);
>         sqlMap.commitTransaction();
>     } catch (SQLException e) {
>         try {
>             sqlMap.rollbackTransaction();
>         } catch (SQLException ex) {
>             throw new DaoException(ex.fillInStackTrace());
>         }
>         throw new DaoException(e.fillInStackTrace());
>     }
>     return result;
> }
> 
> Is it necessary to have a transaction started for just 1 statement
> execution?
> 
> Also, what's the better way? Doing a transaction in a Service class, that
> has multiple DAO's, or doing it in the DAO class, doing different statements
> in one method? Or is there no difference?
> 
> 
>

Re: transactions

Posted by Clinton Begin <cl...@gmail.com>.
As great as that tutorial was for iBATIS 1.x, it holds little value for the 
2.0 framework.

For one, the new exception handling paradigm is completely different.

Please see the Tutorial available at www.ibatis.com
<http://www.ibatis.com>and JPetStore 4.

Cheers,
Clinton

On 5/24/05, Lieven De Keyzer <li...@hotmail.com> wrote:
> 
> At http://www.reumann.net/struts/ibatisLesson1/step6.do
> this is an example in a ibatis/struts tutorial
> 
> public int update(String statementName, Object parameterObject) throws
> DaoException {
> int result = 0;
> try {
> sqlMap.startTransaction();
> result = sqlMap.executeUpdate(statementName, parameterObject);
> sqlMap.commitTransaction();
> } catch (SQLException e) {
> try {
> sqlMap.rollbackTransaction();
> } catch (SQLException ex) {
> throw new DaoException(ex.fillInStackTrace());
> }
> throw new DaoException(e.fillInStackTrace());
> }
> return result;
> }
> 
> Is it necessary to have a transaction started for just 1 statement
> execution?
> 
> Also, what's the better way? Doing a transaction in a Service class, that
> has multiple DAO's, or doing it in the DAO class, doing different 
> statements
> in one method? Or is there no difference?
> 
> 
>