ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Mitchell, Steven C" <Steven.Mitch...@umb.com>
Subject RE: Transaction Best Practices
Date Mon, 03 Oct 2005 20:59:46 GMT
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 !=
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
"!= null" is written "== null" or something.  

The end result would look something like this:

private final DaoManager daoManager;
private final CompanyDao companyDao;

public SomeConstructor () {
  daoManager = DaoHelper.getDaoManager();
  companyDao = (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 =  DaoHelper.getDaoManager();

            final CompanyDao companyDao = ( 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 = null;

        try

        {

            daoManager = DaoHelper.getDaoManager();

            daoManager.startTransaction();

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

            companyDao.updateCompany( company );

            daoManager.commitTransaction();            

        }

        catch ( Exception e )

        {

            throw new IVRPasswordException( e );

        }

        finally

        {

            if ( daoManager != null )

            {

                daoManager.endTransaction();

            }

        }

    }

 

Thoughts?

 

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

 

 


Mime
View raw message