You are viewing a plain text version of this content. The canonical link for it is here.
Posted to slide-dev@jakarta.apache.org by ms...@apache.org on 2002/02/05 05:07:12 UTC
cvs commit: jakarta-slide/src/stores/slidestore/j2ee J2EEDescriptorsStore.java
msmith 02/02/04 20:07:12
Modified: src/stores/slidestore/j2ee J2EEDescriptorsStore.java
Log:
Add the database test statements and calls to connectIfNeededAndPossible()
to the J2EEDescriptorStore as well - now the store will reconnect to the
database if the connection stops working (if, for example, the socket
times out or is otherwise invalidated).
Revision Changes Path
1.2 +62 -9 jakarta-slide/src/stores/slidestore/j2ee/J2EEDescriptorsStore.java
Index: J2EEDescriptorsStore.java
===================================================================
RCS file: /home/cvs/jakarta-slide/src/stores/slidestore/j2ee/J2EEDescriptorsStore.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- J2EEDescriptorsStore.java 24 Dec 2001 22:08:13 -0000 1.1
+++ J2EEDescriptorsStore.java 5 Feb 2002 04:07:12 -0000 1.2
@@ -1,7 +1,7 @@
/*
- * $Header: /home/cvs/jakarta-slide/src/stores/slidestore/j2ee/J2EEDescriptorsStore.java,v 1.1 2001/12/24 22:08:13 remm Exp $
- * $Revision: 1.1 $
- * $Date: 2001/12/24 22:08:13 $
+ * $Header: /home/cvs/jakarta-slide/src/stores/slidestore/j2ee/J2EEDescriptorsStore.java,v 1.2 2002/02/05 04:07:12 msmith Exp $
+ * $Revision: 1.2 $
+ * $Date: 2002/02/05 04:07:12 $
*
* ====================================================================
*
@@ -91,7 +91,7 @@
*
* @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
* @author Ashok Kumar
- * @version $Revision: 1.1 $
+ * @version $Revision: 1.2 $
*/
public class J2EEDescriptorsStore
@@ -256,6 +256,11 @@
return statements;
}
+
+ protected String getDatabaseConnectionTestStatement()
+ {
+ return "select 1 from objects where uri is null";
+ }
/**
* Initializes the data source with a set of parameters.
@@ -289,6 +294,9 @@
getLogger().log(e.toString(),LOG_CHANNEL,Logger.ERROR);
throw new ServiceConnectionFailedException(this, e);
}
+ catch(NullPointerException e) {
+ e.printStackTrace(System.err);
+ }
// all updates must be done inside a transaction, no auto commits
try {
@@ -335,8 +343,19 @@
throw new ServiceDisconnectionFailedException(this, e);
}
}
-
-
+
+ /**
+ * Tries to reconnect if needed but doesn't report failure.
+ */
+ protected synchronized void connectIfNeededAndPossible() {
+ try {
+ connectIfNeeded();
+ }
+ catch (Throwable ex) {
+ // ignore
+ }
+ }
+
/**
* Initializes data source.
* <p/>
@@ -449,7 +468,18 @@
public boolean isConnected()
throws ServiceAccessException {
try {
- return ((connection != null) && (!connection.isClosed()));
+ if ((connection == null) || (connection.isClosed())) {
+ return false;
+ }
+
+ PreparedStatement statement =
+ connection.prepareStatement(getDatabaseConnectionTestStatement());
+ statement.executeQuery();
+ statement.close();
+
+ // testStatement executed without throwing an exception
+ return true;
+
} catch (SQLException e) {
throw new ServiceAccessException(this, e);
}
@@ -501,10 +531,14 @@
if (!alreadyEnlisted)
{
try {
-// getLogger().log("start",LOG_CHANNEL,Logger.DEBUG);
+ // getLogger().log("start",LOG_CHANNEL,Logger.DEBUG);
+
+ // test connection
+ connectIfNeeded();
+
// discard changes made outside a tranaction
connection.rollback();
- } catch (SQLException e) {
+ } catch (Exception e) {
throw new XAException(XAException.XAER_RMERR);
}
alreadyEnlisted=true;
@@ -620,6 +654,7 @@
} catch (SQLException e) {
getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
+ connectIfNeededAndPossible();
throw new ServiceAccessException(this, e);
} finally {
closeStatement(statement);
@@ -706,6 +741,7 @@
} catch (SQLException e) {
getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
+ connectIfNeededAndPossible();
throw new ServiceAccessException(this, e);
} finally {
closeStatement(statement);
@@ -791,6 +827,7 @@
} catch (SQLException e) {
getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
+ connectIfNeededAndPossible();
throw new ServiceAccessException(this, e);
} finally {
closeStatement(statement);
@@ -842,6 +879,7 @@
} catch (SQLException e) {
getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
+ connectIfNeededAndPossible();
throw new ServiceAccessException(this, e);
}
}
@@ -884,6 +922,7 @@
statement.execute();
} catch (SQLException e) {
getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
+ connectIfNeededAndPossible();
throw new ServiceAccessException(this, e);
} finally {
closeStatement(statement);
@@ -924,6 +963,7 @@
statement.execute();
} catch (SQLException e) {
getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
+ connectIfNeededAndPossible();
throw new ServiceAccessException(this, e);
} finally {
closeStatement(statement);
@@ -951,6 +991,7 @@
statement.execute();
} catch (SQLException e) {
getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
+ connectIfNeededAndPossible();
throw new ServiceAccessException(this, e);
} finally {
closeStatement(statement);
@@ -999,6 +1040,7 @@
} catch (SQLException e) {
getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
+ connectIfNeededAndPossible();
throw new ServiceAccessException(this, e);
} finally {
closeStatement(statement);
@@ -1043,6 +1085,7 @@
statement.execute();
} catch (SQLException e) {
getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
+ connectIfNeededAndPossible();
throw new ServiceAccessException(this, e);
} finally {
closeStatement(statement);
@@ -1095,6 +1138,7 @@
} catch (SQLException e) {
getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
+ connectIfNeededAndPossible();
throw new ServiceAccessException(this, e);
} finally {
closeStatement(statement);
@@ -1131,6 +1175,7 @@
} catch (SQLException e) {
getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
+ connectIfNeededAndPossible();
throw new ServiceAccessException(this, e);
} finally {
closeStatement(statement);
@@ -1197,6 +1242,7 @@
} catch (SQLException e) {
getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
+ connectIfNeededAndPossible();
throw new ServiceAccessException(this, e);
} finally {
closeStatement(statement);
@@ -1305,6 +1351,7 @@
} catch (SQLException e) {
getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
+ connectIfNeededAndPossible();
throw new ServiceAccessException(this, e);
} finally {
closeStatement(statement);
@@ -1372,6 +1419,7 @@
} catch (SQLException e) {
getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
+ connectIfNeededAndPossible();
throw new ServiceAccessException(this, e);
} finally {
closeStatement(statement);
@@ -1438,6 +1486,7 @@
} catch (SQLException e) {
getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
+ connectIfNeededAndPossible();
throw new ServiceAccessException(this, e);
} finally {
closeStatement(statement);
@@ -1527,6 +1576,7 @@
} catch (SQLException e) {
getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
+ connectIfNeededAndPossible();
throw new ServiceAccessException(this, e);
} finally {
closeStatement(statement);
@@ -1606,6 +1656,7 @@
} catch (SQLException e) {
getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
+ connectIfNeededAndPossible();
throw new ServiceAccessException(this, e);
} finally {
closeStatement(statement);
@@ -1672,6 +1723,7 @@
} catch (SQLException e) {
getLogger().log(e,LOG_CHANNEL,Logger.ERROR);
+ connectIfNeededAndPossible();
throw new ServiceAccessException(this, e);
} finally {
closeStatement(statement);
@@ -1691,6 +1743,7 @@
try {
statement.close();
} catch (SQLException e) {
+ connectIfNeededAndPossible();
}
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>