Une connection JDBC avec reconnexion automatique
Projet gus05 de développement Java :: Développements :: Quelque-unes de mes entités :: Programmation JDBC :: Gestion des objets Connection
Page 1 sur 1
Une connection JDBC avec reconnexion automatique
Voici une entité qui permet de créer des objets java.sql.Connection qui possèdent la propriété de se reconnecter automatiquement dans le cas où ceux-ci sont utilisés et que la connexion au serveur s'est malheureusement terminée après un temps prolongé d'inactivité.
Dans le code ci-dessous, la classe Connection1 qui implémente java.sql.Connection est un bon exemple de "lazy wrapping", technique pour laquelle je rédigerai un tutoriel prochainement.
L'entité gus.database.connection.buildnewconnection est une transformation qui prend en entrée un String[] de taille 3, contenant les informations url, user, password et renvoie en sortie l'objet Connection. Fonctionnellement, elle équivaut à :
Connection cx = DriverManager.getConnection(url,user,password);
Son code source est composé des deux classes ci-dessous :
Dans le code ci-dessous, la classe Connection1 qui implémente java.sql.Connection est un bon exemple de "lazy wrapping", technique pour laquelle je rédigerai un tutoriel prochainement.
L'entité gus.database.connection.buildnewconnection est une transformation qui prend en entrée un String[] de taille 3, contenant les informations url, user, password et renvoie en sortie l'objet Connection. Fonctionnellement, elle équivaut à :
Connection cx = DriverManager.getConnection(url,user,password);
Son code source est composé des deux classes ci-dessous :
- Code:
package gus05.entity.gus.database.connection.buildnewconnection;
import gus05.framework.core.Entity;
import gus05.framework.features.Transform;
public class BuildNewConnection implements Entity, Transform {
public String getName() {return "gus.database.connection.buildnewconnection";}
public String getCreationDate() {return "2011.11.18";}
public Object transform(Object obj) throws Exception
{
String[] info = (String[]) obj;
if(info.length!=3) throw new Exception("Wrong info number: "+info.length);
return new Connection1(info[0],info[1],info[2]);
}
}
- Code:
package gus05.entity.gus.database.connection.buildnewconnection;
import java.awt.Toolkit;
import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;
public class Connection1 implements Connection {
private String url;
private String user;
private String password;
private Connection cx;
private boolean closed;
public Connection1(String url, String user, String password)
{
this.url = url;
this.user = user;
this.password = password;
closed = false;
}
private Connection cx() throws SQLException
{
if(cx==null) // lazy connection
{
cx = buildCx();
if(closed) cx.close();
}
else if(cx.isClosed()) // reconnection
{
if(!closed) cx = buildCx();
}
return cx;
}
private Connection buildCx() throws SQLException
{
if(user==null || user.equals(""))
return DriverManager.getConnection(url);
return DriverManager.getConnection(url,user,password);
}
public void close() throws SQLException
{
if(cx!=null && !cx.isClosed()) cx.close();
closed = true;
}
public boolean isClosed() throws SQLException
{return closed;}
public boolean isWrapperFor(Class<?> iface) throws SQLException
{return cx().isWrapperFor(iface);}
public <T> T unwrap(Class<T> iface) throws SQLException
{return cx().unwrap(iface);}
public void abort(Executor executor) throws SQLException
{cx().abort(executor);}
public void clearWarnings() throws SQLException
{cx().clearWarnings();}
public void commit() throws SQLException
{cx().commit();}
public Array createArrayOf(String typeName, Object[] elements) throws SQLException
{return cx().createArrayOf(typeName, elements);}
public Blob createBlob() throws SQLException
{return cx().createBlob();}
public Clob createClob() throws SQLException
{return cx().createClob();}
public NClob createNClob() throws SQLException
{return cx().createNClob();}
public SQLXML createSQLXML() throws SQLException
{return cx().createSQLXML();}
public Statement createStatement() throws SQLException
{return cx().createStatement();}
public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException
{return cx().createStatement(resultSetType,resultSetConcurrency);}
public Statement createStatement(int resultSetType,int resultSetConcurrency, int resultSetHoldability)throws SQLException
{return cx().createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);}
public Struct createStruct(String typeName, Object[] attributes)throws SQLException
{return cx().createStruct(typeName, attributes);}
public boolean getAutoCommit() throws SQLException
{return cx().getAutoCommit();}
public String getCatalog() throws SQLException
{return cx().getCatalog();}
public Properties getClientInfo() throws SQLException
{return cx().getClientInfo();}
public String getClientInfo(String name) throws SQLException
{return cx().getClientInfo(name);}
public int getHoldability() throws SQLException
{return cx().getHoldability();}
public DatabaseMetaData getMetaData() throws SQLException
{return cx().getMetaData();}
public int getNetworkTimeout() throws SQLException
{return cx().getNetworkTimeout();}
public String getSchema() throws SQLException
{return cx().getSchema();}
public int getTransactionIsolation() throws SQLException
{return cx().getTransactionIsolation();}
public Map<String, Class<?>> getTypeMap() throws SQLException
{return cx().getTypeMap();}
public SQLWarning getWarnings() throws SQLException
{return cx().getWarnings();}
public boolean isReadOnly() throws SQLException
{return cx().isReadOnly();}
public boolean isValid(int timeout) throws SQLException
{return cx().isValid(timeout);}
public String nativeSQL(String sql) throws SQLException
{return cx().nativeSQL(sql);}
public CallableStatement prepareCall(String sql) throws SQLException
{return cx().prepareCall(sql);}
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException
{return cx().prepareCall(sql, resultSetType, resultSetConcurrency);}
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)throws SQLException
{return cx().prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability);}
public PreparedStatement prepareStatement(String sql) throws SQLException
{return cx().prepareStatement(sql);}
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException
{return cx().prepareStatement(sql, autoGeneratedKeys);}
public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException
{return cx().prepareStatement(sql, columnIndexes);}
public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException
{return cx().prepareStatement(sql, columnNames);}
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException
{return cx().prepareStatement(sql, resultSetType, resultSetConcurrency);}
public PreparedStatement prepareStatement(String sql, int resultSetType,int resultSetConcurrency, int resultSetHoldability)throws SQLException
{return cx().prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability);}
public void releaseSavepoint(Savepoint savepoint) throws SQLException
{cx().releaseSavepoint(savepoint);}
public void rollback() throws SQLException
{cx().rollback();}
public void rollback(Savepoint savepoint) throws SQLException
{cx().rollback(savepoint);}
public void setAutoCommit(boolean autoCommit) throws SQLException
{cx().setAutoCommit(autoCommit);}
public void setCatalog(String catalog) throws SQLException
{cx().setCatalog(catalog);}
public void setClientInfo(Properties properties) throws SQLClientInfoException
{if(cx!=null) cx.setClientInfo(properties);}
public void setClientInfo(String name, String value)throws SQLClientInfoException
{if(cx!=null) cx.setClientInfo(name, value);}
public void setHoldability(int holdability) throws SQLException
{cx().setHoldability(holdability);}
public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException
{cx().setNetworkTimeout(executor, milliseconds);}
public void setReadOnly(boolean readOnly) throws SQLException
{cx().setReadOnly(readOnly);}
public Savepoint setSavepoint() throws SQLException
{return cx().setSavepoint();}
public Savepoint setSavepoint(String name) throws SQLException
{return cx().setSavepoint(name);}
public void setSchema(String schema) throws SQLException
{cx().setSchema(schema);}
public void setTransactionIsolation(int level) throws SQLException
{cx().setTransactionIsolation(level);}
public void setTypeMap(Map<String, Class<?>> map) throws SQLException
{cx().setTypeMap(map);}
}
Projet gus05 de développement Java :: Développements :: Quelque-unes de mes entités :: Programmation JDBC :: Gestion des objets Connection
Page 1 sur 1
Permission de ce forum:
Vous ne pouvez pas répondre aux sujets dans ce forum
|
|
Mar 16 Sep - 12:01 par Gus
» Présentation du jeu Vindinium
Jeu 20 Fév - 15:32 par Gus
» Rechercher la cible la plus proche avec le "Breadth First Search"
Jeu 20 Fév - 13:06 par Gus
» Impression d'écran avec sélection de zone
Jeu 20 Fév - 12:12 par Gus
» Envoyer un mail par un compte Gmail
Jeu 25 Avr - 14:04 par Gus
» Streaming : comment télécharger les vidéos
Lun 4 Fév - 19:59 par Gus
» Synology : installer ipkg
Mar 22 Jan - 21:22 par Gus
» Trouver le type de lecteur avec JNA
Mer 9 Jan - 23:11 par Gus
» Adresse ip publique et adresse ip privée, Internet box et UPnP
Mer 9 Jan - 21:02 par Gus
» Accéder au numéro de série du lecteur par un script vb
Mer 9 Jan - 19:31 par Gus