1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Vector;
/**
* @author
*
* Cette classe permet de gérer les connections au serveur Domino
*
*/
public class DominoConnectionHandler
{
// Url d'accès au serveur Domino
private static String connStr = ConfigHandler.getParameter("route_Domino");
// Pool de connections. Il s'agit d'une liste de connections
private static Vector pool;
// Liste des etats des connections
private static Vector state;
// Taille du pool de connection
private static final int POOL_SIZE = Integer.parseInt(ConfigHandler.getParameter("nb_connexions_domino"));
/**
*
* Renvoie une connection au serveur Domino. Cette connection provient du
* pool s'il en reste des libres. Sinon elle est crée et ajoutée au pool.
*
* @return une connection au serveur Domino.
*/
public static synchronized Connection getConnection()
{
for (int i = 0; i < state.size(); i++)
{
if (state.get(i).equals("FREE"))
{
state.set(i, "USED");
return (Connection) pool.get(i);
}
}
// Aucune connection n'est libre ---> ajout d'une connexion
// supplementaire au pool
try
{
Class.forName("lotus.jdbc.domino.DominoDriver");
Connection connection = DriverManager.getConnection(connStr, "", "");
pool.add(connection);
state.add("USED");
return connection;
}
catch (ClassNotFoundException e)
{
Logger.traceDebug("ClassNotFoundExecption: " + e.getMessage());
}
catch (SQLException e)
{
Logger.traceDebug("SQLException: " + e.getMessage());
}
return null;
}
/**
*
* Libère une connection en la restituant au pool de connection et en la
* marquant comme 'free'
*
* @param connection
* connection à libérer
* @throws Exception
* si une erreur intervient pendant la restitution de la
* connection
*/
public static void freeConnection(Connection connection) throws Exception
{
int index = pool.indexOf(connection);
Logger.traceDebug("Libération de la connection " + index);
state.set(index, "FREE");
}
/**
* Initialise le pool de connections en créant le nombre de connections
* indiqué dans la variable POOL_SIZE et en les stockant dans une liste.
* Toutes ces connections sont alors marquées comme 'free'
*/
public static void init()
{
try
{
pool = new Vector();
state = new Vector();
// Le pool de connexion est créé uniquement si le paramètre POOL
// est à true dans le fichier de configuration.
if (ConfigHandler.getParameter("POOL").equals("true"))
{
Class.forName("lotus.jdbc.domino.DominoDriver");
Logger.traceDebug("Creation du Pool de connection");
Logger.traceDebug("Url de connexion : " + connStr);
for (int i = 0; i < POOL_SIZE; i++)
{
Connection connection = DriverManager.getConnection(connStr, "", "");
pool.add(connection);
state.add("FREE");
}
Logger.traceDebug("Pool de connections Domino créé (nombre de connexions : " + POOL_SIZE + ")");
}
}
catch (ClassNotFoundException e)
{
Logger.traceDebug("ClassNotFoundExecption: " + e.getMessage());
}
catch (SQLException e)
{
Logger.traceDebug("SQLException: " + e.getMessage());
}
}
} |
Partager