Return-Path: Delivered-To: apmail-ibatis-user-java-archive@www.apache.org Received: (qmail 80633 invoked from network); 3 Oct 2005 21:00:12 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 3 Oct 2005 21:00:12 -0000 Received: (qmail 71965 invoked by uid 500); 3 Oct 2005 21:00:10 -0000 Delivered-To: apmail-ibatis-user-java-archive@ibatis.apache.org Received: (qmail 71943 invoked by uid 500); 3 Oct 2005 21:00:09 -0000 Mailing-List: contact user-java-help@ibatis.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: user-java@ibatis.apache.org Delivered-To: mailing list user-java@ibatis.apache.org Received: (qmail 71932 invoked by uid 99); 3 Oct 2005 21:00:09 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 03 Oct 2005 14:00:09 -0700 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests=HTML_MESSAGE X-Spam-Check-By: apache.org Received-SPF: pass (asf.osuosl.org: local policy) Received: from [198.136.201.198] (HELO dnsmail3.umb.com) (198.136.201.198) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 03 Oct 2005 14:00:14 -0700 Received: from y8107a.umb.corp.umb.com (viruswall2.umb.com [192.168.3.213]) by dnsmail3.umb.com (8.12.10+Sun/8.12.11) with ESMTP id j93KxluV016789 for ; Mon, 3 Oct 2005 15:59:47 -0500 (CDT) Received: from y6005a.umb.corp.umb.com ([172.19.43.34]) by y8107a.umb.corp.umb.com with InterScan Messaging Security Suite; Mon, 03 Oct 2005 15:59:47 -0500 X-MimeOLE: Produced By Microsoft Exchange V6.0.6603.0 content-class: urn:content-classes:message MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----_=_NextPart_001_01C5C85D.62FEF59C" Subject: RE: Transaction Best Practices Date: Mon, 3 Oct 2005 15:59:46 -0500 Message-ID: X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: Transaction Best Practices Thread-Index: AcXGSDnRwxP5uJ4WS92Qa/33OpVjNgCFMmEQ From: "Mitchell, Steven C" To: X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N This is a multi-part message in MIME format. ------_=_NextPart_001_01C5C85D.62FEF59C Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: quoted-printable One more question. How does iBatis know when to close the connections with the SIMPLE data source? I thought it was when the DaoManager when out of scope. Currently, our Controller classes (where that code is from) use all static methods. If my assumption about scope is correct, then we will need to switch to new instances of each Controller with non-static methods to implement your suggestion, correct? =20 =20 -----Original Message----- From: Clinton Begin [mailto:clinton.begin@gmail.com]=20 Sent: Saturday, October 01, 2005 12:23 AM To: user-java@ibatis.apache.org Subject: Re: Transaction Best Practices =20 Question: which DataSource (i.e. connection pool) are you using? There's no need to explicitly open/close transactions for queries. =20 It all looks good. One thing I will suggest is to guarantee that daoManager can never be null...that will avoid the "if (daoManager !=3D null)" part...it's unecessary code. And unless you're doing something really strange, you might as well just make the DAOs private members of the class too...initialize them in the constructor (this also makes unit testing easier, because you can inject mock DAOs if necessary). So far my recommendations are stylistic, but they will reduce the amount of boilerplate code you're writing, and perhaps help you to find the one place you're not calling endTransaction (or perhaps the one place that "!=3D null" is written "=3D=3D null" or something. =20 The end result would look something like this: private final DaoManager daoManager; private final CompanyDao companyDao; public SomeConstructor () { daoManager =3D DaoHelper.getDaoManager(); companyDao =3D (CompanyDao) daoManager.getDao(CompanyDao.class); =20 } public static void updateCompany (Company company)=20 throws IVRPasswordException { try { daoManager.startTransaction(); companyDao.updateCompany( company ); daoManager.commitTransaction(); =20 } catch (Exception e) { throw new IVRPasswordException(e); } finally { daoManager.endTransaction(); } } On 9/30/05, Mitchell, Steven C wrote: I've used iBatis on many projects now. My latest project has run out of Oracle connections a couple of time during testing, which has me concerned. There did not appear to be any kind of looping going on. I found only one Controller method that called multiple methods that used the same DAO. I changed the parent method to get the DAO and pass it into the two child methods. =20 =20 Now, I'm wondering if I am using Transactions as they were indented. I've only used Transaction on updates. Should I use them on reads to make sure everything gets cleaned up. Here is an example of what I do: =20 // NO TRANSACTION FOR QUERIES =20 public static Company getCompany( Integer companyId ) throws IVRPasswordException {=20 try { final DaoManager daoManager =3D DaoHelper.getDaoManager(); final CompanyDao companyDao =3D ( CompanyDao ) daoManager.getDao( CompanyDao.class ); return companyDao.getCompany( companyId ); } catch ( Exception e ) { throw new IVRPasswordException( e ); } } =20 // TRANSACTION FOR ALL UPDATES/INSERTS/DELTES =20 public static void updateCompany( Company company ) throws IVRPasswordException { DaoManager daoManager =3D null; try { daoManager =3D DaoHelper.getDaoManager(); daoManager.startTransaction(); final CompanyDao companyDao =3D (CompanyDao) daoManager.getDao( CompanyDao.class ); companyDao.updateCompany( company ); daoManager.commitTransaction(); =20 } catch ( Exception e ) { throw new IVRPasswordException( e ); } finally { if ( daoManager !=3D null ) { daoManager.endTransaction(); } } } =20 Thoughts? =20 Steve Mitchell Group Leader for Java Development UMB Bank, n.a. =20 =20 ------_=_NextPart_001_01C5C85D.62FEF59C Content-Type: text/html; charset="US-ASCII" Content-Transfer-Encoding: quoted-printable

One more question.  How does = iBatis know when to close the connections with the SIMPLE data source?  I = thought it was when the DaoManager when out of scope.  Currently, our Controller = classes (where that code is from) use all static methods.  If my assumption = about scope is correct, then we will need to switch to new instances of each = Controller with non-static methods to implement your suggestion, correct?  =

 

-----Original = Message-----
From: Clinton Begin [mailto:clinton.begin@gmail.com]
Sent:
Saturday, October 01, 2005 12:23 = AM
To: = user-java@ibatis.apache.org
Subject: Re: Transaction = Best Practices

 

Question:  which DataSource (i.e. connection pool) are you using?

There's no need to explicitly open/close transactions for queries.  =

It all looks good.  One thing I will suggest is to guarantee that daoManager can never be null...that will avoid the "if (daoManager = !=3D null)" part...it's unecessary code.

And unless you're doing something really strange, you might as well just = make the DAOs private members of the class too...initialize them in the constructor  (this also makes unit testing easier, because you can = inject mock DAOs if necessary).

So far my recommendations are stylistic, but they will reduce the amount = of boilerplate code you're writing, and perhaps help you to find the one = place you're not calling endTransaction (or perhaps the one place that = "!=3D null" is written "=3D=3D null" or something. 

The end result would look something like this:

private final DaoManager daoManager;
private final CompanyDao companyDao;

public SomeConstructor () {
  daoManager =3D DaoHelper.getDaoManager();
  companyDao =3D (CompanyDao) = daoManager.getDao(CompanyDao.class); 
}

public static void updateCompany (Company company)
    throws IVRPasswordException {
  try {
    daoManager.startTransaction();
    companyDao.updateCompany( company );
    daoManager.commitTransaction();       =    
  } catch (Exception e) {
    throw new IVRPasswordException(e);
  } finally {
    daoManager.endTransaction();
  }
}

On = 9/30/05, Mitchell, Steven C <Steven.Mitchell@umb.com> wrote:

I've used iBatis on many projects now.  = My latest project has run out of Oracle connections a couple of time during testing, which has me concerned. There did not appear to be any kind of = looping going on.  I found only one Controller method that called multiple = methods that used the same DAO.  I changed the parent method to get the DAO = and pass it into the two child methods. 

 

Now, I'm wondering if I am using Transactions = as they were indented.  I've only used Transaction on updates.  Should = I use them on reads to make sure everything gets cleaned up.  Here is an = example of what I do:

 

// NO TRANSACTION FOR = QUERIES

 

public static Company getCompany( Integer = companyId )

    throws = IVRPasswordException

    {

        = try

        = {

        = ;    final DaoManager daoManager =3D =  DaoHelper.getDaoManager();

        = ;    final CompanyDao companyDao =3D ( CompanyDao ) daoManager.getDao( CompanyDao.class );

        = ;    return companyDao.getCompany( companyId );

        = }

        = catch ( Exception e )

        = {

        = ;    throw new IVRPasswordException( e );

        = }

    }

 

// TRANSACTION FOR ALL = UPDATES/INSERTS/DELTES

 

public static void updateCompany( Company = company ) throws IVRPasswordException

    {

        = DaoManager daoManager =3D null;

        = try

  =       {

        = ;    daoManager =3D DaoHelper.getDaoManager();

        = ;    daoManager.startTransaction();

        = ;    final CompanyDao companyDao =3D (CompanyDao) daoManager.getDao( = CompanyDao.class );

        = ;    companyDao.updateCompany( company );

        = ;    daoManager.commitTransaction();       =     

        = }

        = catch ( Exception e )

        = {

        = ;    throw new IVRPasswordException( e );

        = }

        = finally

        = {

        = ;    if ( daoManager !=3D null )

        = ;    {

        = ;        daoManager.endTransaction();

        = ;    }

        = }

    }

 

Thoughts?

 

Steve Mitchell
Group Leader for Java Development
UMB Bank, n.a.

 

 

------_=_NextPart_001_01C5C85D.62FEF59C--