IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

JDBC Java Discussion :

pb de gestion de pool avec c3p0 et spring 2


Sujet :

JDBC Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Nouveau membre du Club
    Développeur informatique
    Inscrit en
    Octobre 2006
    Messages
    7
    Détails du profil
    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Octobre 2006
    Messages : 7
    Par défaut pb de gestion de pool avec c3p0 et spring 2
    Bonjour,

    Je developpe actullement un petite application web destinée à administrer un base de donnée sous postgresql. j'ai utiliser pour ce c3p0 comme gestionnaire de pool de connexion, spring 2 pour l'IOC, et le tout tourne sous tomcat 5.5.17.

    Mes classes de la couche DAO sont de la forme :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
     
    package dao.postgresql;
     
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Types;
    import java.util.Collection;
    import java.util.List;
     
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.jdbc.core.RowMapper;
    import org.springframework.jdbc.core.RowMapperResultSetExtractor;
    import org.springframework.jdbc.core.support.JdbcDaoSupport;
     
    import dao.DaoException;
    import dao.IDaoAdministrateurs;
    import entities.Administrateurs;
     
    public class DaoAdministrateurs extends JdbcDaoSupport implements IDaoAdministrateurs {
    	public Collection getAllAdministrateurs() {
    		JdbcTemplate jt = getJdbcTemplate();
    		String sqlquery = "SELECT admin.*, count(DISTINCT ws.ws_url) AS nbr_ws FROM administrateurs AS admin " +
    				" LEFT OUTER JOIN websites AS ws ON admin.admin_login = ws.admin_login " +
    				" GROUP BY admin.admin_login, admin.admin_password, admin.admin_dtcrea, admin.admin_email, admin.admin_nom, admin.admin_prenom, admin.admin_isadmin" +
    				" ORDER BY admin.admin_login, admin.admin_nom, admin.admin_prenom";
    		List result = (List) jt.query(sqlquery, new RowMapperResultSetExtractor(new AdministrateurRowMapper()));
    		closeConn();
    		if(result == null || result.size() == 0) {
    			throw new DaoException("Not Found", 1);
    		} else {
    			return result;
    		}
    	}
     
     
    	public Administrateurs getOneById(String adminLogin) {
    		JdbcTemplate jt = getJdbcTemplate();
    		String sqlquery = "SELECT admin.*, count(DISTINCT ws.ws_url) AS nbr_ws FROM administrateurs AS admin " +
    				" LEFT OUTER JOIN websites AS ws ON admin.admin_login = ws.admin_login " +
    				" WHERE admin.admin_login = ? " +
    				" GROUP BY admin.admin_login, admin.admin_password, admin.admin_dtcrea, admin.admin_email, admin.admin_nom, admin.admin_prenom, admin.admin_isadmin" +
    				" ORDER BY admin.admin_login, admin.admin_nom, admin.admin_prenom";
    		Object [] args = new Object [] { adminLogin };
    		int [] types = new int [] { Types.VARCHAR };
    		try {
    			Administrateurs admin = (Administrateurs) jt.queryForObject(sqlquery, args, types, new AdministrateurRowMapper());
    			closeConn();
    			return admin;
    		} catch (Exception e) {
    			closeConn();
    			throw new DaoException("Not Found", 1);
    		}
    	}
     
    	public Administrateurs getOneByEmail(String adminEmail) {
    		JdbcTemplate jt = getJdbcTemplate();
    		String sqlquery = "SELECT admin.*, count(DISTINCT ws.ws_url) AS nbr_ws FROM administrateurs AS admin " +
    				" LEFT OUTER JOIN websites AS ws ON admin.admin_login = ws.admin_login " +
    				" WHERE admin.admin_email = ? " +
    				" GROUP BY admin.admin_login, admin.admin_password, admin.admin_dtcrea, admin.admin_email, admin.admin_nom, admin.admin_prenom, admin.admin_isadmin" +
    				" ORDER BY admin.admin_login, admin.admin_nom, admin.admin_prenom";
    		Object [] args = new Object [] { adminEmail };
    		int [] types = new int [] { Types.VARCHAR };
    		try {
    			Administrateurs admin = (Administrateurs) jt.queryForObject(sqlquery, args, types, new AdministrateurRowMapper());
    			closeConn();
    			return admin;
    		} catch (Exception e) {
    			closeConn();
    			throw new DaoException("Not Found", 1);
    		}
    	}
     
    	public void saveOne(Administrateurs admin) {
    		JdbcTemplate jt = getJdbcTemplate();
    		String sqlquery = "INSERT INTO administrateurs (admin_login, admin_password, admin_email, admin_dtcrea, admin_nom, admin_prenom, admin_isadmin) VALUES (?, ?, ?, ?, ?, ?, ?)";
    		Object[] args = new Object[7];
    		args[0] = admin.getAdminLogin();
    		args[1] = admin.getAdminPassword();
    		args[2] = admin.getAdminEmail();
    		args[3] = admin.getAdminDtcrea();
    		if(null != admin.getAdminNom()) {
    			args[4] = admin.getAdminNom();
    		} else {
    			args[4] = null;
    		}
    		if(null != admin.getAdminPrenom()) {
    			args[5] = admin.getAdminPrenom();
    		} else {
    			args[5] = null;
    		}
    		args[6] = admin.isAdminIsadmin();
    		int [] types = new int [] {Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.TIMESTAMP, Types.VARCHAR, Types.VARCHAR, Types.BOOLEAN};
    		try {
    			int res = jt.update(sqlquery, args, types);
    			closeConn();
    			if(res != 1) {
    				throw new DaoException("Erreur d'insertion", 2);
    			}
    		} catch (Exception e) {
    			closeConn();
    			throw new DaoException("Erreur d'insertion ("+e.getMessage()+")", 2);
    		}
    	}
     
    	public void updateOne(Administrateurs admin) {
    		JdbcTemplate jt = getJdbcTemplate();
    		String sqlquery = "UPDATE administrateurs SET admin_password = ?, admin_email = ?, admin_dtcrea = ?, admin_nom = ?, admin_prenom = ?, admin_isadmin = ? WHERE admin_login = ?";
    		Object[] args = new Object[7];
    		args[0] = admin.getAdminPassword();
    		args[1] = admin.getAdminEmail();
    		args[2] = admin.getAdminDtcrea();
    		if(null != admin.getAdminNom()) {
    			args[3] = admin.getAdminNom();
    		} else {
    			args[3] = null;
    		}
    		if(null != admin.getAdminPrenom()) {
    			args[4] = admin.getAdminPrenom();
    		} else {
    			args[4] = null;
    		}
    		args[5] = admin.isAdminIsadmin();
    		args[6] = admin.getAdminLogin();
    		int [] types = new int [] {Types.VARCHAR, Types.VARCHAR, Types.TIMESTAMP, Types.VARCHAR, Types.VARCHAR, Types.BOOLEAN, Types.VARCHAR};
    		try {
    			int res = jt.update(sqlquery, args, types);
    			closeConn();
    			if(res != 1) {
    				throw new DaoException("Erreur de modification", 3);
    			}
    		} catch (Exception e) {
    			closeConn();
    			throw new DaoException("Erreur de modification ("+e.getMessage()+")", 3);
    		}
    	}
     
    	public void deleteOne(Administrateurs admin) {
    		JdbcTemplate jt = getJdbcTemplate();
    		String sqlquery = "DELETE FROM administrateurs WHERE admin_login = ?";
    		Object [] args = new Object [] { admin.getAdminLogin() };
    		int [] types = new int [] { Types.VARCHAR };
    		try {
    			int res = jt.update(sqlquery, args, types);
    			closeConn();
    			if(res != 1) {
    				throw new DaoException("Erreur de suppression", 4);
    			}
    		} catch (Exception e) {
    			closeConn();
    			throw new DaoException("Erreur de suppression ("+e.getMessage()+")", 4);
    		}
    	}
     
    	class AdministrateurRowMapper implements RowMapper {
    		public Object mapRow(ResultSet rs, int index) throws SQLException {
    			Administrateurs admin = new Administrateurs();
    			admin.setAdminLogin(rs.getString("admin_login"));
    			admin.setAdminPassword(rs.getString("admin_password"));
    			admin.setAdminEmail(rs.getString("admin_email"));
    			admin.setAdminDtcrea(rs.getTimestamp("admin_dtcrea"));
    			admin.setAdminNom(rs.getString("admin_nom"));
    			admin.setAdminPrenom(rs.getString("admin_prenom"));
    			admin.setAdminIsadmin(rs.getBoolean("admin_isadmin"));
    			admin.setNbrWebsites(rs.getInt("nbr_ws"));
    			return admin;
    		}		
    	}
     
    	public void closeConn() {
    		try {
    			getDataSource().getConnection().close();
    		} catch (SQLException e) {
    			System.err.println(e.getMessage());
    		}
    	}
    }
    et je dois avoir une bonne trentaine de classes comme ça.

    Mon fichier spring-config.xml est de la forme :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans SYSTEM "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>
    	<bean id="myDS" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    		<property name="driverClass">
                <value>org.postgresql.Driver</value>
            </property>
    		<property name="jdbcUrl">
    <value>jdbc:postgresql://localhost:5432/${dbname}</value>
    		</property>
    		<property name="acquireIncrement" value="0"/>
    		<property name="initialPoolSize" value="1"/>
    		<property name="minPoolSize" value="1"/>
    		<property name="maxPoolSize" value="2"/>
    		<property name="maxIdleTime" value="30"/>
    		<property name="maxIdleTimeExcessConnections" value="30"/>
    		<property name="idleConnectionTestPeriod" value="30"/>
    		<property name="maxStatements" value="0"/>
    		<property name="maxStatementsPerConnection" value="0"/>
    		<property name="maxConnectionAge" value="40"/>
    		<property name="properties">
    			<props>
    				<prop key="user">${dbuser}</prop>
    				<prop key="c3p0.acquireIncrement">0</prop>
    				<prop key="c3p0.initialPoolSize">1</prop>
                    <prop key="c3p0.minPoolSize">1</prop>
    				<prop key="c3p0.maxPoolSize">2</prop>
    				<prop key="c3p0.maxIdleTime">30</prop>
    				<prop key="c3p0.maxIdleTimeExcessConnections">30</prop>
    				<prop key="c3p0.idleConnectionTestPeriod">30</prop>
    				<prop key="c3p0.maxStatements">0</prop>
    				<prop key="c3p0.maxStatementsPerConnection">0</prop>
    				<prop key="c3p0.maxConnectionAge">40</prop>
    				<prop key="acquireIncrement">0</prop>
    				<prop key="initialPoolSize">1</prop>
                    <prop key="minPoolSize">1</prop>
    				<prop key="maxPoolSize">2</prop>
    				<prop key="maxIdleTime">30</prop>
    				<prop key="maxIdleTimeExcessConnections">30</prop>
    				<prop key="idleConnectionTestPeriod">30</prop>
    				<prop key="maxStatements">0</prop>
    				<prop key="maxStatementsPerConnection">0</prop>
    				<prop key="maxConnectionAge">40</prop>
                    <prop key="password">${dbpass}</prop>
    			</props>
    		</property>
    	</bean>
    	<!-- sessionFatory pour JDBC -->
    	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    		<property name="dataSource">
    			<ref local="myDS"/>
    		</property>
    	</bean>
    	<bean id="AwareTransactionDataSource" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
    		<property name="targetDataSource">
    			<ref bean="myDS"/>
    		</property>
    	</bean>
    	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    		<property name="dataSource">
    			<ref bean="AwareTransactionDataSource"/>
    		</property>
    	</bean>
    	<bean id="daoIAdministrateurs" class="dao.postgresql.DaoAdministrateurs">
    		<property name="jdbcTemplate">
    			<ref bean="jdbcTemplate" />
    		</property>
    	</bean>
    	<bean id="daoAdministrateurs" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    		<property name="transactionManager">
    			<ref local="transactionManager"/>
    		</property>
    		<property name="target">
    			<ref local="daoIAdministrateurs"/>
    		</property>
    		<property name="transactionAttributes">
    			<props>
    				<prop key="save*">PROPAGATION_REQUIRED</prop>
    				<prop key="update*">PROPAGATION_REQUIRED</prop>
    				<prop key="del*">PROPAGATION_REQUIRED</prop>
    			</props>
    		</property>
    	</bean>
    	<bean id="serviceAdministrateurs" class="services.ServiceAdministrateurs">
    		<property name="daoAdministrateurs">
    			<ref bean="daoAdministrateurs"/>
    		</property>		
    	</bean>
     
    ...
     
    </beans>

    Mon application fonctionne bien pendant un moment puis commence à repondre de plus en plus lentement au fur et à mesur de l'utilisation

    Sachant que je developpe sous linux, au lancement de tomcat, quand je liste les processus avec la commande ps aux, au départ je n'ai que 2 ou trois connexions ouvertes au niveau de la base de donné, mais au bout d'un certain temps d'utilisation, je me retrouve avec une soixantaine de connexions ouvertes sur postgres.
    Je cherche à comprendre où est l'erreur que j'ai commis lors de la fermeture de la connexion au niveau des classes de la couche DAO ou au niveau du parametrage de c3p0 dans le fichier spring-config.xml

    si qq pouvais m'aider, ça méviterai de devoir relancer tomcat 1 fois par heure
    merci

  2. #2
    Membre confirmé
    Inscrit en
    Avril 2003
    Messages
    288
    Détails du profil
    Informations personnelles :
    Âge : 50

    Informations forums :
    Inscription : Avril 2003
    Messages : 288
    Par défaut
    Salut,

    J'ai développé une classe de gestion de pool de connexion, mais je sais pas si ça peut t'intérresser...
    Ca marche apparemment pour tout types de BDD (j'ai testé que sur Oracle, DB2 et mysql).

    Tiens moi au courant par MP si ça t'intérresse car c'est trop volumineux pour le poster ici

    ++

  3. #3
    Nouveau membre du Club
    Développeur informatique
    Inscrit en
    Octobre 2006
    Messages
    7
    Détails du profil
    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Octobre 2006
    Messages : 7
    Par défaut
    bon, je vien d'essayer en remplaçant la datasource comme suit:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
     
    <bean id="myDS" class="org.postgresql.jdbc3.Jdbc3PoolingDataSource" destroy-method="close">
    		<property name="serverName">
    			<value>localhost</value>
    		</property>
    		<property name="databaseName">
    			<value>${dbname}</value>
    		</property>
    		<property name="portNumber">
    			<value>5432</value>
    		</property>
    		<property name="user">
    			<value>${dbuser}</value>
    		</property>
    		<property name="password">
    			<value>${dbpass}</value>
    		</property>
    		<property name="initialConnections">
    			<value>2</value>
    		</property>   
    		<property name="maxConnections">
    			<value>10</value>
    		</property>
    </bean>
    mon application tourne toujours normelement, mais j'ai toujours une multitude de connexion ouvertes sur mon postgres après un certain temps d'utilisation :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    postgres 23364  0.0  0.3  20680  1936 ?        S    20:17   0:00 postgres: dbuser dbname 127.0.0.1(41556) idle
    et je sais pertinement qu'au bout de deux heure je vais me retrouver avec des exception ressmblents à celles que j'avais lorsque j'utilisais apache commons-dbcp :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Could not open JDBC Connection for transaction; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (FATAL: D?sol?, trop de clients sont d?j? connect?s)
    ce qui me chiffone, c'est que j'ai mis maxConnections = 10 pour Jdbc3PoolingDataSource et que en 6 cliques, je me retrouve avec 60 connexion ouvertes et Idle, d'autant plus que par defaut, postgresql accepte 100 connexions max, et que si je veux augmenter ce nombre ça va boufer encore plus de ram

  4. #4
    Membre confirmé
    Inscrit en
    Avril 2003
    Messages
    288
    Détails du profil
    Informations personnelles :
    Âge : 50

    Informations forums :
    Inscription : Avril 2003
    Messages : 288
    Par défaut
    Regardes ma classe et dis moi ce que tu en penses.

    Si ça marche pour ta base et surtout que ça corrige ton problème ... je la posterai dans la rubrique source ou tutoriel pour les autres personnes du forum.

    ++

Discussions similaires

  1. Gestion des Pool de connexion avec Apache
    Par Opsse dans le forum Général Java
    Réponses: 2
    Dernier message: 15/06/2015, 20h57
  2. Pool de connection avec C3P0 sans hibernate
    Par peofofo dans le forum JDBC
    Réponses: 0
    Dernier message: 10/04/2012, 10h32
  3. Gestion de formulaire avec xml et sans serveur ?
    Par meliane dans le forum XML/XSL et SOAP
    Réponses: 2
    Dernier message: 05/05/2004, 20h57
  4. probleme de gestion de clients avec des sockets....
    Par ludvo dans le forum Réseau
    Réponses: 6
    Dernier message: 25/09/2003, 13h37
  5. [TFrame] Problème de gestion du OnMouseDown avec une Frame
    Par xherault dans le forum Composants VCL
    Réponses: 5
    Dernier message: 23/05/2003, 16h35

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo