You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by János Löbb <ja...@yale.edu> on 2011/02/24 16:49:54 UTC

programming question

Hi,

What is the very basic structure of a web application that is connected to a database through a connection pool, but would not require to restart itself or restart Tomcat when the database goes down - let say for maintenance ?

Telling otherwise how to write a webapp that would survive a database recycling and would not require human interactions to make it work again.

Thanks ahead,

János



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: programming question

Posted by Jorge Medina <ce...@gmail.com>.
If you use Oracle, some DBCP settings may not work and you may need to
use Oracle connection pool classes.
In particular, I was not able to use DBCP and have a loginTimeout when
using Oracle.

Using Oracle connection pool classes, the validation query does not
work in the same way as in Apache DBCP. Apache DBCP tries to give you
a valid connection if the query fails. Oracle will just test and throw
an exception indicating the connection is stale (as opposed to just
giving you the stale connection). So even with an Oracle RAC, the pool
may return a stale connection or throw an exception. So, I use Oracle
Connection Caching settings to grow a pool from 0 to n and discard any
connection that has been idle more than T seconds. This was worked
well in production environment. (without having to switch to Oracle
UCP, which was our next option).


On Fri, Feb 25, 2011 at 3:17 PM, Christopher Schultz
<ch...@christopherschultz.net> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> David,
>
> On 2/24/2011 12:08 PM, David Smith wrote:
>> With tomcat's built-in database pooling, just adding a validation query
>> to the resource config should be all that's necessary.  On each borrow
>> of a connection, the connection is tested and closed if the test fails.
>> Failed connections are replaced with new ones.
>
> +1
>
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.10 (MingW32)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEYEARECAAYFAk1oDkQACgkQ9CaO5/Lv0PB4ywCffleVFm8jZtJRcD3x2V3gCs6T
> DBUAoKUD6zJXXPURMoQLtm8SGecxQfqk
> =MkzS
> -----END PGP SIGNATURE-----
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: programming question

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

David,

On 2/24/2011 12:08 PM, David Smith wrote:
> With tomcat's built-in database pooling, just adding a validation query
> to the resource config should be all that's necessary.  On each borrow
> of a connection, the connection is tested and closed if the test fails. 
> Failed connections are replaced with new ones.

+1

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk1oDkQACgkQ9CaO5/Lv0PB4ywCffleVFm8jZtJRcD3x2V3gCs6T
DBUAoKUD6zJXXPURMoQLtm8SGecxQfqk
=MkzS
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: programming question

Posted by David Smith <da...@cornell.edu>.
With tomcat's built-in database pooling, just adding a validation query
to the resource config should be all that's necessary.  On each borrow
of a connection, the connection is tested and closed if the test fails. 
Failed connections are replaced with new ones.

--David

On 2/24/2011 10:49 AM, János Löbb wrote:
> Hi,
>
> What is the very basic structure of a web application that is connected to a database through a connection pool, but would not require to restart itself or restart Tomcat when the database goes down - let say for maintenance ?
>
> Telling otherwise how to write a webapp that would survive a database recycling and would not require human interactions to make it work again.
>
> Thanks ahead,
>
> János
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: programming question

Posted by David kerber <dc...@verizon.net>.
On 2/24/2011 11:35 AM, chris derham wrote:
>>
>> When I mention them that their programs should handle the situation
>> resulting from the database bounce automagically and not the dba handling
>> the web app or Tomcat shutdowns and restarts, they look at me like I came
>> from Mars or Saturn :-)
>>
>
> If you code the app to use a connection pool, the connections are cached.
> The connection pool can be configured to test the connection, and remove it
> from the pool if it has been closed. In this way as the app asks for a
> connection, if the database has been restarted, old connections will be
> dropped and new ones created.

That's what I do, but I handle it in my app instead of using DBCP.


> Various different ways to configure the connection pool - in the app or in
> the container. We use DBCP and works fine for us in the scenario you
> describe
>
> Just search google for tomcat database connection pool.
>
> HTH
>
> Chris
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: programming question

Posted by chris derham <ch...@derham.me.uk>.
>
> When I mention them that their programs should handle the situation
> resulting from the database bounce automagically and not the dba handling
> the web app or Tomcat shutdowns and restarts, they look at me like I came
> from Mars or Saturn :-)
>

If you code the app to use a connection pool, the connections are cached.
The connection pool can be configured to test the connection, and remove it
from the pool if it has been closed. In this way as the app asks for a
connection, if the database has been restarted, old connections will be
dropped and new ones created.

Various different ways to configure the connection pool - in the app or in
the container. We use DBCP and works fine for us in the scenario you
describe

Just search google for tomcat database connection pool.

HTH

Chris

Re: programming question

Posted by Charles Polisher <cp...@surewest.net>.
Mark Eggers wrote:
> When the database is shut down, I get exception messages logged (you do log 
> exceptions, right?). Pages that depend on database content are missing that 
> content.
> 
> When the database is started up again, a refresh of the pages displays the 
> content coming from the database.
> 
> Better handling of the exception (bubbling up to the controller level) would 
> allow me to display an appropriate error or maintenance page.

Or, you could do what $PRODUCT (million U.S. dollar Portal product)
does: start writing to your log in a tight loop and never come back.
I'm not bitter...
-- 
Charles Polisher

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: programming question

Posted by Mark Eggers <it...@yahoo.com>.
----- Original Message (edited) ----

From: Michael Ludwig <mi...@gmx.de>

János Löbb schrieb am 24.02.2011 um 11:24 (-0500):
> On Feb 24, 2011, at 10:58 AM, David kerber wrote:
> > On 2/24/2011 10:49 AM, János Löbb wrote:
> >> 
> >> What is the very basic structure of a web application that is
> >> connected to a database through a connection pool, but would not
> >> require to restart itself or restart Tomcat when the database goes
> >> down - let say for maintenance ?
> >> 
> >> Telling otherwise how to write a webapp that would survive a
> >> database recycling and would not require human interactions to make
> >> it work again.
> > 
> > I've never had trouble having my app reconnect after restarting the
> > database; the tomcat db connection classes handle that
> > transparently, AFAIK.
> > 
> 
> Well, I have here programmers who are writing web applications and
> those are stuck if I recycle the database.  Therefore I have to do a 1
> hour hokusz-pokusz, shutting down Tomcat, etc... all over the place,
> before I can recycle the database.   So I just would like to know what
> would be the structure or skeleton of the simplest java web app that
> would not die if the database is pulled underneath and would reconnect
> like charm after the database is back again.  

Many or most database applications can't do business when the database
is not available. All write operations will fail unless, for example,
going to a message queue. All read operations will fail unless, for
example, tapping into a cache, or another database. What's left to do
for the app when it cannot offer its services to the user? Basically,
display a "temporarily out-of-service" page.

> When I mention them that their programs should handle the situation
> resulting from the database bounce automagically and not the dba
> handling the web app or Tomcat shutdowns and restarts, they look at me
> like I came from Mars or Saturn :-)
> 
> So, I just want to know how it is done in the "real world" :-)

I don't know. Various procedures seem possible.

* static maintenance downtime page
* fallback app offering basic functionality reading from a cache
* offering more advanced functionality, writing to a queue

Questionable whether the complexities involved in the latter approaches
will not far outweigh the benefit to the user. I don't think there is a
trivial solution that does more than display a maintenance page.

-- 
Michael Ludwig


----- Original Message (edited) ----

A conceptual framework for doing this could be:

1. Use Tomcat's provided connection pooling
2. Use a validationQuery in Resource element of context.xml
3. Trap the exceptions when the database is down
4. Handle database connection exceptions by displaying a maintenance page

When the database comes back up, the validationQuery will reestablish database 
connectivity and your application should work.

I have a sample application that I use for resource leak testing. It uses Derby 
as the database, and the context.xml file looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true">
    <Resource
        name="jdbc/leakrs"
        description="Leaks R Us"
        type="javax.sql.DataSource"
        auth="Container"
        driverClassName="org.apache.derby.jdbc.ClientDriver"
        maxActive="10"
        maxIdle="3"
        maxWait="10000"
        password="*****"
        url="jdbc:derby://localhost:1527/leakrs"
        validationQuery="values(1)"
        username="*****"/>
</Context>

When the database is shut down, I get exception messages logged (you do log 
exceptions, right?). Pages that depend on database content are missing that 
content.

When the database is started up again, a refresh of the pages displays the 
content coming from the database.

Better handling of the exception (bubbling up to the controller level) would 
allow me to display an appropriate error or maintenance page.

. . . . . just my two cents.

/mde/


      

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: programming question

Posted by Martin Gainty <mg...@hotmail.com>.
michael is correct..if your db goes down there is nowhere to persist your data objects to and there is nowhere to query the data from

Have you considered writing to a database cluster?

MySQL Clustering:
http://dev.mysql.com/doc/refman/5.1/en/mysql-cluster-replication.html

Oracle RAC:
http://www.oracle.com/technetwork/database/clustering/overview/index.html

Any other Database clustering technology?
Martin--
______________________________________________ 
Verzicht und Vertraulichkeitanmerkung/Note de déni et de confidentialité
 
Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich dem Austausch von Informationen und entfaltet keine rechtliche Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von E-Mails koennen wir keine Haftung fuer den Inhalt uebernehmen.
Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le destinataire prévu, nous te demandons avec bonté que pour satisfaire informez l'expéditeur. N'importe quelle diffusion non autorisée ou la copie de ceci est interdite. Ce message sert à l'information seulement et n'aura pas n'importe quel effet légalement obligatoire. Étant donné que les email peuvent facilement être sujets à la manipulation, nous ne pouvons accepter aucune responsabilité pour le contenu fourni.




> Date: Fri, 25 Feb 2011 20:09:00 +0100
> From: milu71@gmx.de
> To: users@tomcat.apache.org
> Subject: Re: programming question
> 
> János Löbb schrieb am 24.02.2011 um 11:24 (-0500):
> > On Feb 24, 2011, at 10:58 AM, David kerber wrote:
> > > On 2/24/2011 10:49 AM, János Löbb wrote:
> > >> 
> > >> What is the very basic structure of a web application that is
> > >> connected to a database through a connection pool, but would not
> > >> require to restart itself or restart Tomcat when the database goes
> > >> down - let say for maintenance ?
> > >> 
> > >> Telling otherwise how to write a webapp that would survive a
> > >> database recycling and would not require human interactions to make
> > >> it work again.
> > > 
> > > I've never had trouble having my app reconnect after restarting the
> > > database; the tomcat db connection classes handle that
> > > transparently, AFAIK.
> > > 
> > 
> > Well, I have here programmers who are writing web applications and
> > those are stuck if I recycle the database.  Therefore I have to do a 1
> > hour hokusz-pokusz, shutting down Tomcat, etc... all over the place,
> > before I can recycle the database.   So I just would like to know what
> > would be the structure or skeleton of the simplest java web app that
> > would not die if the database is pulled underneath and would reconnect
> > like charm after the database is back again.  
> 
> Many or most database applications can't do business when the database
> is not available. All write operations will fail unless, for example,
> going to a message queue. All read operations will fail unless, for
> example, tapping into a cache, or another database. What's left to do
> for the app when it cannot offer its services to the user? Basically,
> display a "temporarily out-of-service" page.
> 
> > When I mention them that their programs should handle the situation
> > resulting from the database bounce automagically and not the dba
> > handling the web app or Tomcat shutdowns and restarts, they look at me
> > like I came from Mars or Saturn :-)
> > 
> > So, I just want to know how it is done in the "real world" :-)
> 
> I don't know. Various procedures seem possible.
> 
> * static maintenance downtime page
> * fallback app offering basic functionality reading from a cache
> * offering more advanced functionality, writing to a queue
> 
> Questionable whether the complexities involved in the latter approaches
> will not far outweigh the benefit to the user. I don't think there is a
> trivial solution that does more than display a maintenance page.
> 
> -- 
> Michael Ludwig
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 
 		 	   		  

Re: programming question

Posted by Michael Ludwig <mi...@gmx.de>.
János Löbb schrieb am 24.02.2011 um 11:24 (-0500):
> On Feb 24, 2011, at 10:58 AM, David kerber wrote:
> > On 2/24/2011 10:49 AM, János Löbb wrote:
> >> 
> >> What is the very basic structure of a web application that is
> >> connected to a database through a connection pool, but would not
> >> require to restart itself or restart Tomcat when the database goes
> >> down - let say for maintenance ?
> >> 
> >> Telling otherwise how to write a webapp that would survive a
> >> database recycling and would not require human interactions to make
> >> it work again.
> > 
> > I've never had trouble having my app reconnect after restarting the
> > database; the tomcat db connection classes handle that
> > transparently, AFAIK.
> > 
> 
> Well, I have here programmers who are writing web applications and
> those are stuck if I recycle the database.  Therefore I have to do a 1
> hour hokusz-pokusz, shutting down Tomcat, etc... all over the place,
> before I can recycle the database.   So I just would like to know what
> would be the structure or skeleton of the simplest java web app that
> would not die if the database is pulled underneath and would reconnect
> like charm after the database is back again.  

Many or most database applications can't do business when the database
is not available. All write operations will fail unless, for example,
going to a message queue. All read operations will fail unless, for
example, tapping into a cache, or another database. What's left to do
for the app when it cannot offer its services to the user? Basically,
display a "temporarily out-of-service" page.

> When I mention them that their programs should handle the situation
> resulting from the database bounce automagically and not the dba
> handling the web app or Tomcat shutdowns and restarts, they look at me
> like I came from Mars or Saturn :-)
> 
> So, I just want to know how it is done in the "real world" :-)

I don't know. Various procedures seem possible.

* static maintenance downtime page
* fallback app offering basic functionality reading from a cache
* offering more advanced functionality, writing to a queue

Questionable whether the complexities involved in the latter approaches
will not far outweigh the benefit to the user. I don't think there is a
trivial solution that does more than display a maintenance page.

-- 
Michael Ludwig

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: programming question

Posted by János Löbb <ja...@yale.edu>.
On Feb 24, 2011, at 10:58 AM, David kerber wrote:

> On 2/24/2011 10:49 AM, János Löbb wrote:
>> Hi,
>> 
>> What is the very basic structure of a web application that is connected to a database through a connection pool, but would not require to restart itself or restart Tomcat when the database goes down - let say for maintenance ?
>> 
>> Telling otherwise how to write a webapp that would survive a database recycling and would not require human interactions to make it work again.
> 
> I've never had trouble having my app reconnect after restarting the database; the tomcat db connection classes handle that transparently, AFAIK.
> 

Well, I have here programmers who are writing web applications and those are stuck if I recycle the database.  Therefore I have to do a 1 hour hokusz-pokusz, shutting down Tomcat, etc... all over the place, before I can recycle the database.   So I just would like to know what would be the structure or skeleton of the simplest java web app that would not die if the database is pulled underneath and would reconnect like charm after the database is back again.  

When I mention them that their programs should handle the situation resulting from the database bounce automagically and not the dba handling the web app or Tomcat shutdowns and restarts, they look at me like I came from Mars or Saturn :-)

So, I just want to know how it is done in the "real world" :-)

Thanks ahead,

János



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: programming question

Posted by David kerber <dc...@verizon.net>.
On 2/24/2011 10:49 AM, János Löbb wrote:
> Hi,
>
> What is the very basic structure of a web application that is connected to a database through a connection pool, but would not require to restart itself or restart Tomcat when the database goes down - let say for maintenance ?
>
> Telling otherwise how to write a webapp that would survive a database recycling and would not require human interactions to make it work again.

I've never had trouble having my app reconnect after restarting the 
database; the tomcat db connection classes handle that transparently, AFAIK.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org