personnaly i used informations from here
http://cwiki.apache.org/CAY/setting-database-connection.html
I inherit the cayenne PoolManager (code is attached) and when a
disconnect occurs i use a thread to poll when
the connection becomes active with
try {
_log.trace("[DatabaseConnectionMonitor:run()] Try to get a
connection from PoolManager.");
//Get a connection and close it immediately
OpconPoolManager.getConnection().close();
_log.trace("[DatabaseConnectionMonitor:run()] Connection to
database established !");
} catch (SQLException e) {
//Event Disconnected already sent so do nothing
_log.trace("[DatabaseConnectionMonitor:run()] Cannot get a
connection from database.");
}
Colin Bankier wrote:
Hi,
I can't seem to find any examples of how to properly close a database
connection and clear any related cache.
My app is using an embedded database, of which it can download a new version
and overwrite the old one.
At the moment I try to clear any cache using context.getQueryCache().clear()
(context is my DataContext instance).
And then set the context to null (for want of anything better to do).
The app then downloads and copies a new version of the database over the
old, and connects using context = DataContext.createDataContext() etc etc.
However - the data displayed STILL is the old data - until the app is
shutdown and re-started when it correctly reads the new data.
Any help on how to properly close the database connection would be greatly
appreciated.
Cheers,
Colin Bankier.
package com.sma.core.net;
import java.lang.ref.WeakReference;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import javax.sql.ConnectionPoolDataSource;
import org.apache.cayenne.conn.PoolManager;
import com.sma.core.IDatabaseEventListener;
/**
* OpconPoolManager is a wrapper around PoolManager
* the main goal is to alerts members when a connection event occurs
* Use addConnectionListener to become a member and receive connection events
*/
public class OpconPoolManager extends PoolManager {
public static final int MIN_CONNECTIONS = 1;
public static final int MAX_CONNECTIONS = 5;
private boolean _isConnected = true;
public OpconPoolManager(ConnectionPoolDataSource poolDataSource,
int minCons, int maxCons, String userName, String password)
throws SQLException {
super(poolDataSource, minCons, maxCons, userName, password);
}
public OpconPoolManager(ConnectionPoolDataSource poolDataSource, String userName, String password)
throws SQLException {
super(poolDataSource, MIN_CONNECTIONS, MAX_CONNECTIONS, userName, password);
}
/**
* Internal ConnectionEventListener list to alert when a connection event arrives.
*/
private Collection<WeakReference<IDatabaseEventListener>> _listenerList =
Collections.synchronizedSet(new HashSet<WeakReference<IDatabaseEventListener>>());
/**
* Add a listener, so it will be contacted on connection event
* @param eventListener
*/
public void addConnectionListener(IDatabaseEventListener eventListener)
{
// Use WeakReference to avoid memory leak if this becomes the
// sole reference to the object.
for (WeakReference<IDatabaseEventListener> ref : _listenerList) {
IDatabaseEventListener listener = ref.get();
//referenced object doesn't exit anymore so remove it
if (listener == null)
_listenerList.remove(ref);
else if (listener == eventListener)
return;
}
WeakReference<IDatabaseEventListener> reference = new WeakReference<IDatabaseEventListener> (eventListener);
_listenerList.add(reference);
}
/**
* Remove the listener, so it will not be contacted on event
* @param eventListener
*/
public void removeConnectionListener(IDatabaseEventListener eventListener)
{
// Use WeakReference to avoid memory leak if this becomes the
// sole reference to the object.
for (WeakReference<IDatabaseEventListener> ref : _listenerList) {
IDatabaseEventListener listener = ref.get();
if (listener == null)
_listenerList.remove(ref);
else if (listener == eventListener)
{
_listenerList.remove(ref);
break;
}
}
}
/**
* Send <code>onReconnect</code> if event is null or <code>onDisconnect</code>
* @param event pass null send onReconnect()
*/
private void sendEvent(SQLException event)
{
for (WeakReference<IDatabaseEventListener> ref : _listenerList) {
IDatabaseEventListener listener = ref.get();
if (listener == null) {
//referenced object doesnt exit anymore so remove it
_listenerList.remove(ref);
}
else {
if (event == null)
listener.onReconnect();
else
listener.onDisconnect(event);
}
}
}
/**
* Inform all listeners that the database connection
* is not available anymore
* @param event the disconnection source
*/
public void fireDisconnect(SQLException event)
{
this.sendEvent(event);
}
/**
* Inform all listeners that the database connection is available
*/
public void fireReconnect()
{
this.sendEvent(null);
}
public String getConnectionStatus()
{
return ( "Connections Used: " + this.getCurrentlyInUse() +
", Unused: " + this.getCurrentlyUnused() +
", Max: " + this.getMaxConnections() );
}
@Override
public synchronized Connection getConnection(String user, String pass) throws SQLException {
try {
Connection conn = super.getConnection(user, pass);
if (conn != null)
{
if (_isConnected == false)
this.fireReconnect();
_isConnected = true;
}
return conn;
} catch (SQLException e) {
if (_isConnected == true)
this.fireDisconnect(e);
_isConnected = false;
throw e;
}
}
}