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 Clinton Begin <cl...@gmail.com> on 2005/10/01 07:22:47 UTC

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 <St...@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.
>
>