Projet gus05 de développement Java
Vous souhaitez réagir à ce message ? Créez un compte en quelques clics ou connectez-vous pour continuer.
-29%
Le deal à ne pas rater :
PC portable Gamer ERAZER DEPUTY P60 – 15,6” FHD 144Hz – i7-12è ...
999.99 € 1399.99 €
Voir le deal

Une connection JDBC avec reconnexion automatique

Aller en bas

Une connection JDBC avec reconnexion automatique Empty Une connection JDBC avec reconnexion automatique

Message  Gus Ven 18 Nov - 13:28

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 :

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);}
}
Gus
Gus
Admin

Messages : 249
Date d'inscription : 01/09/2009

http://www.gus05.com

Revenir en haut Aller en bas

Revenir en haut

- Sujets similaires

 
Permission de ce forum:
Vous ne pouvez pas répondre aux sujets dans ce forum