Best practice has nothing to do w/ struts. On Struts wiki there are several DAO's you can pick from.

Struts allows MVC layers, so that you can unit test each layer. For example unit test the model and dao.
Even w/in a DAO... you never use a connection.
So I recommend you select a dao, and unit test the dao layer (outside of struts).

The books that talk about datasource and connections are very old.
Use a DAO! ex: Apache iBatis and iBatis example PetStore.

.V

emre akbas wrote:
Hi all,
In a well written struts application, we assume declarative exception
handling is used. Therefore the code of the action classes will look clean
and short (and nice) because all the exception handling is done via the
exception-handler classes outside. My question is about db connection
management when declarative exception handling is used.

A usual scenario is :

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public class MyAction extends Action {
...
public ActionForward execute( . .. ) {
..
Connection con = getDataSource().getConnection();

// an exception occurs here and the control passes to the
// exception-handler class, which can never close the connection
// opened above.
....

con.close();
}
...
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

What to do in a situation like above? Since the exception-handler could not
close the connection opened in the Action class, this will cause the
connections in the pool to be exhausted. To prevent the situation above, we
may :

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public class MyAction extends Action {
...
public ActionForward execute( . .. ) {
..
Connection con = null;
try {
con = getDataSource().getConnection();

// an exception occurs here
....

} catch(MyAppSpecifiException e) {
....
} catch(Exception e) { // IMPORTANT, this line will catch all the exceptions
....
} finally {
con.close();
}

}
...
}


The scheme above seems to solve the unclosed connection problem but it does
not use declarative exception-handling and the code is really messed-up with
exception handling statements. It does not look nice, clean and short.

The situation is more complicated when you call a service method within the
action and that service method throws an exception.

I would like to hear your points on this issuse. Thanks in advance.


--

Emre Akbas



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to