Hallo ihr,
ich habe eine Anwendung die rund um die Uhr laufen muss.
Leider bricht die Datenbankverbindung regelmäßig ab.
Um eine Verbindung mit der Datenbank zu erstellen verwende ich einen Connection Pool mit der Bibiliothek commons-dbcp, die ich im Internet gefunden habe.
Ich habe gelesen das eine connection, wenn die wait_timeout vom Datenbankserver abgelaufen ist, geschlossen wird. Mit getConnection() wird immer eine funktionierende Connection aus dem Pool geholt. Die nicht mehr funktionierenden Verbindungen bleiben aktiv und werden nicht automatisch aus dem Pool entfernt. Wenn dann die maximale Poolgröße erreicht wird geht gar nichts mehr.
Meine Frage ist jetzt, wie kann ich aus dem Pool eine kaputte Verbindung entfernen und wie kann ich diese Identifizieren?
Hat einer eine Idee, eine Anregung oder eine Alternative?
Hier die Klasse über die ich die Verbindung erstelle:
ich habe eine Anwendung die rund um die Uhr laufen muss.
Leider bricht die Datenbankverbindung regelmäßig ab.
Um eine Verbindung mit der Datenbank zu erstellen verwende ich einen Connection Pool mit der Bibiliothek commons-dbcp, die ich im Internet gefunden habe.
Ich habe gelesen das eine connection, wenn die wait_timeout vom Datenbankserver abgelaufen ist, geschlossen wird. Mit getConnection() wird immer eine funktionierende Connection aus dem Pool geholt. Die nicht mehr funktionierenden Verbindungen bleiben aktiv und werden nicht automatisch aus dem Pool entfernt. Wenn dann die maximale Poolgröße erreicht wird geht gar nichts mehr.
Meine Frage ist jetzt, wie kann ich aus dem Pool eine kaputte Verbindung entfernen und wie kann ich diese Identifizieren?
Hat einer eine Idee, eine Anregung oder eine Alternative?
Hier die Klasse über die ich die Verbindung erstelle:
Java:
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* [url]http://www.apache.org/licenses/LICENSE-2.0[/url]
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.sql.DriverManager;
import java.sql.Connection;
//
// Here are the dbcp-specific classes.
// Note that they are only used in the setupDriver
// method. In normal use, your classes interact
// only with the standard JDBC API
//
import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.PoolingDriver;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.dbcp.DriverManagerConnectionFactory;
//
// Here's a simple example of how to use the PoolingDriver.
// In this example, we'll construct the PoolingDriver manually,
// just to show how the pieces fit together, but you could also
// configure it using an external conifguration file in
// JOCL format (and eventually Digester).
//
//
// To compile this example, you'll want:
// * commons-pool-1.5.4.jar
// * commons-dbcp-1.2.2.jar
// in your classpath.
//
// To run this example, you'll want:
// * commons-collections.jar
// * commons-pool-1.5.4.jar
// * commons-dbcp-1.2.2.jar
// * the classes for your (underlying) JDBC driver
// in your classpath.
//
// Invoke the class using two arguments:
// * the connect string for your underlying JDBC driver
// * the query you'd like to execute
// You'll also want to ensure your underlying JDBC driver
// is registered. You can use the "jdbc.drivers"
// property to do this.
//
// For example:
// java -Djdbc.drivers=oracle.jdbc.driver.OracleDriver \
// -classpath commons-pool-1.5.3.jar:commons-dbcp-1.2.2.jar:oracle-jdbc.jar:. \
// ManualPoolingDriverExample \
// "jdbc:oracle:thin:scott/tiger@myhost:1521:mysid" \
// "SELECT * FROM DUAL"
//
public class ManualPoolingDriver {
public ManualPoolingDriver() {
//
// First we load the underlying JDBC driver.
// You need this if you don't use the jdbc.drivers
// system property.
//
System.out.println("Loading underlying JDBC driver.");
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.println("Done.");
//
// Then we set up and register the PoolingDriver.
// Normally this would be handled auto-magically by
// an external configuration, but in this example we'll
// do it manually.
//
System.out.println("Setting up driver.");
try {
setupDriver("jdbc:mysql://localhost/blabla");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Done.");
}
public Connection getConnection() {
Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:example");
return conn;
}catch(Exception ex) {
System.err.println(ex.getMessage());
return conn;
}
}
public static void setupDriver(String connectURI) throws Exception {
//
// First, we'll need a ObjectPool that serves as the
// actual pool of connections.
//
// We'll use a GenericObjectPool instance, although
// any ObjectPool implementation will suffice.
//
ObjectPool connectionPool = new GenericObjectPool(null);
//
// Next, we'll create a ConnectionFactory that the
// pool will use to create Connections.
// We'll use the DriverManagerConnectionFactory,
// using the connect string passed in the command line
// arguments.
//
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI,"name", "pw");
//
// Now we'll create the PoolableConnectionFactory, which wraps
// the "real" Connections created by the ConnectionFactory with
// the classes that implement the pooling functionality.
//
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true);
//
// Finally, we create the PoolingDriver itself...
//
Class.forName("org.apache.commons.dbcp.PoolingDriver");
PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
//
// ...and register our pool with it.
//
driver.registerPool("example",connectionPool);
//
// Now we can just use the connect string "jdbc:apache:commons:dbcp:example"
// to access our pool of Connections.
//
}
public static void printDriverStats() throws Exception {
PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
ObjectPool connectionPool = driver.getConnectionPool("example");
System.out.println("NumActive: " + connectionPool.getNumActive());
System.out.println("NumIdle: " + connectionPool.getNumIdle());
}
public static void shutdownDriver() throws Exception {
PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
driver.closePool("example");
}
}