Well...connections are never "closed" so to speak.  They are pooled and remain open.  They would really only be truly closed if the connection became stale/invalid, or if the application was shutdown (or if you called SimpleDataSource.forceCloseAll()).  There's no standard DataSource API for releasing resources. 

Connections are returned to the pool (i.e. "closed") when you call endTransaction() (assuming you called startTransaction), or if you're using automatic transactions, then the connection is returned upon the completion of the method call.

DaoManager should NEVER go out of scope unless your application shuts down.  Otherwise you're probably misusing the DAO framework...you should have only one instance of DaoManager, and you should keep it around for as long as your application is running (think singleton).

Cheers,
Clinton

On 10/3/05, Mitchell, Steven C <[EMAIL PROTECTED]> wrote:

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:[EMAIL PROTECTED]]
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 <[EMAIL PROTECTED]> 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.

 

 


Reply via email to