You can use a database driver that handles the failover for you.
We use c3p0 as our driver of choice as it provides this facility, as well as connection pooling.
An extract from the tomcat 'server.xml' file is as follows...
Code:
<Resource
name="jdbc/trm"
type="com.mchange.v2.c3p0.ComboPooledDataSource"
driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver"
password="password"
user="username"
auth="Container"
description="DB Connection pool for TRM application"
minPoolSize="2"
maxPoolSize="4"
acquireIncrement="1"
factory="org.apache.naming.factory.BeanFactory"
jdbcUrl="jdbc:sqlserver://mainserver:1433;failoverPartner=backupserver;databaseName=nameofyourdatabase;applicationName=appname"
preferredTestQuery="SELECT 'Connection' = 'true'"
testConnectionOnCheckout="true"
/>
The beauty of this approach is that the whole configuration of the pooling/failover is external to your application and your therefore don't have to reinvent it for each new appl that you develop.
If you didn't learn anything today, you weren't paying attention!