You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by emre akbas <em...@gmail.com> on 2005/10/13 16:32:12 UTC

Struts and db connections :: best practice

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

Re: [OT] Re: Struts and db connections :: best practice

Posted by "Frank W. Zammetti" <fz...@omnytex.com>.
On Thu, October 13, 2005 12:37 pm, Dave Newton said:
> Frank W. Zammetti wrote:
>
>> Everyone else raised completely valid points about not doing what your
>> doing in an Action and why you shouldn't, so I'll just try and answer
>> the question :)
>
> Thus ending a fine and noble tradition.
>
> Booo, Frank, booooo!

Yes, sometimes I can be such a killjoy when I'm in my pragmatist mode ;)

> ;)
>
> Dave

Frank

P.S. - At least where I am, Friday is only 9 1/2 hours away... hold on
just a *little* longer Dave! :-) LOL


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


[OT] Re: Struts and db connections :: best practice

Posted by Dave Newton <ne...@pingsite.com>.
Frank W. Zammetti wrote:

> Everyone else raised completely valid points about not doing what your 
> doing in an Action and why you shouldn't, so I'll just try and answer 
> the question :)

Thus ending a fine and noble tradition.

Booo, Frank, booooo!

;)

Dave



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts and db connections :: best practice

Posted by "Frank W. Zammetti" <fz...@omnytex.com>.
Everyone else raised completely valid points about not doing what your 
doing in an Action and why you shouldn't, so I'll just try and answer 
the question :)

* Wrap your call to getConnection() in a try block with only a finally:

   try {
     Connection con = getDataSource().getConnection();
   } finally {
     con.close();
   }

All other exceptions will still bubble up to the global handler.

* Have a base Action that is responsible for getting the connection, and 
for having this finally:

   public class MyBaseAction extends Action {
     public ActionForward execute(...) {
       try {
         Connection con = getDataSource().getConnection();
         realExecute(...,con);
       } finally {
         con.close();
       }
     }
   }

So, all of your actual Actions will extend MyBaseAction instead of 
Action, and will implement realExecute() instead of execute(). 
realExecute() has the same signature as execute(), with the addition of 
the connection being passed in as well.  So, any exception that occurs 
in the actual Action will still invoke the global handler, but the 
connection will always be closed, and your actual Actions remain nice 
and clean.

Then again, as others said, best practices dictate you shouldn't really 
be doing this anyway, but if you have to, these options might help, 
specifically the second I would prefer.

Frank

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
> 

-- 
Frank W. Zammetti
Founder and Chief Software Architect
Omnytex Technologies
http://www.omnytex.com
AIM: fzammetti
Yahoo: fzammetti
MSN: fzammetti@hotmail.com

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts and db connections :: best practice

Posted by Bryon Lape <bs...@grey-net.com>.
I'm not sure about best practices, but I try to avoid database connections
in my actions.  I tend to put this code in the model objects and let them
handle exceptions and so forth.

> 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: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts and db connections :: best practice

Posted by Larry Meadors <lm...@apache.org>.
I would argue that a well-written struts application will *never* (at
least 99.999% of the time) have jdbc code in it.

Larry


On 10/13/05, emre akbas <em...@gmail.com> 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: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts and db connections :: best practice

Posted by Vic Cekvenich <ne...@roomity.com>.
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: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts and db connections :: best practice

Posted by DG...@EvergreenInvestments.com.
Am I experiencing DejaVu?

Links that might interest you:
http://java.sun.com/blueprints/corej2eepatterns/Patterns/BusinessDelegate.html
http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html 
(see Figure 9.1)
http://www.hibernate.org/
http://www.springframework.org/docs/reference/jdbc.html

-Dennis





emre akbas <em...@gmail.com> 
10/13/2005 10:32 AM
Please respond to
"Struts Users Mailing List" <us...@struts.apache.org>


To
Struts Users Mailing List <us...@struts.apache.org>
cc

Subject
Struts and db connections :: best practice






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