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

Frameworks Web Java Discussion :

Configuration MySql avec Hibernate et Spring


Sujet :

Frameworks Web Java

  1. #1
    Membre à l'essai
    Profil pro
    Inscrit en
    Août 2007
    Messages
    48
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Août 2007
    Messages : 48
    Points : 16
    Points
    16
    Par défaut Configuration MySql avec Hibernate et Spring
    Bonjour

    je suis en train de développer une application JavaEE avec MySql, Hibernate et Spring.

    Je rencontre un problème de configuration, que je n'arrive pas a cibler :

    MySql est paramétrée pour accueillir un max de 100 connexions.
    Hibernate devrait gérer un pool de 20 connexions.

    MySql me renvoie systématiquement "Too many connexions" quand j'arrive a une centaine d'accès à la base.

    J'en déduis qu'hibernate n'est pas correctement configuré ?

    my.ini (paramètres par défaut)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
    max_connections=100
    hibernate.cfg.xml
    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
     
    <hibernate-configuration>
        <session-factory>
            <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mydb</property>
            <property name="hibernate.connection.username">root</property>
            <property name="hibernate.connection.password">root</property>
            <property name="hibernate.connection.pool_size">20</property>
            <property name="hibernate.connection.release_mode">auto</property>
     
            <property name="show_sql">false</property>
            <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
     
            <property name="current_session_context_class">thread</property>
            <property name="hibernate.current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</property>
     
            <property name="hibernate.cache.use_query_cache">false</property>
            <property name="hibernate.cache.use_second_level_cache">false</property>
     
            <!-- Mapping files -->
            <mapping resource="fr/vivreaction/hibernate/Action.hbm.xml" />
           .....
        </session-factory>
    </hibernate-configuration>
    Dans mon applicationContext.xml
    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
     
     
    	<!-- sessionFactory  -->
    	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" destroy-method="close">
    		<property name="configLocation">
    			<value>classpath:hibernate.cfg.xml</value>
    		</property>
    	</bean>
     
    	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory" ref="sessionFactory"/>
    	</bean>
    	<bean id="transactionProxy" abstract="true"
    		class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    		<property name="transactionManager" ref="transactionManager"/>
    		<property name="transactionAttributes">
    			<props>
    				<prop key="insert*">PROPAGATION_REQUIRED</prop>
    				<prop key="update*">PROPAGATION_REQUIRED</prop>
    				<prop key="save*">PROPAGATION_REQUIRED</prop>
    				<prop key="*">PROPAGATION_REQUIRED, readOnly</prop>
    			</props>
    		</property>
    	</bean>
     
       .....
    L'exception :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
    	at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
    	at org.hibernate.loader.Loader.doList(Loader.java:2294)
    	at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2172)
    	at org.hibernate.loader.Loader.list(Loader.java:2167)
    	at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:448)
    	at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
    	at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
    	at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1258)
    	at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
     
    ....
     
    Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection,  message from server: "Too many connections"
    Merci de votre aide !

  2. #2
    Expert confirmé
    Profil pro
    Inscrit en
    Août 2006
    Messages
    3 274
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 3 274
    Points : 4 141
    Points
    4 141
    Par défaut
    Tu fermes bien tes sessions après utilisation pour libération du pool ?

  3. #3
    Membre à l'essai
    Profil pro
    Inscrit en
    Août 2007
    Messages
    48
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Août 2007
    Messages : 48
    Points : 16
    Points
    16
    Par défaut
    non.

    j'ai lu qu'en déléguant les trasactions a Spring, je n'avais plus besoin que de faire du getSession().[save/merge]

    je vais remettre getSession().close() dans mes finally alors .. c ca ?

    Merci

  4. #4
    Membre à l'essai
    Profil pro
    Inscrit en
    Août 2007
    Messages
    48
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Août 2007
    Messages : 48
    Points : 16
    Points
    16
    Par défaut
    Re

    en fait ca ne résout pas le problème.

    Voila le DAO que j'appelle pour reproduire mon bug :
    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
     
     
    		Session session = getSession();
    		try {
    			Query query = session.createQuery("from Action");
     
    			List<Action> result = new ArrayList<Action>();
     
    			result.addAll(query.list());
     
    			return result;
     
    		} catch (RuntimeException re) {
    			log.error("get failed", re);
    			throw re;
    		} finally {
    			session.close();
    		}
    mais j'ai mis ce block finally sur toutes mes méthodes DAO.

    Merci d'avance.

  5. #5
    Expert confirmé
    Profil pro
    Inscrit en
    Août 2006
    Messages
    3 274
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 3 274
    Points : 4 141
    Points
    4 141
    Par défaut
    Si tu veux calquer tes sessions sur tes transactions, ne devrais-tu pas utiliser hibernate.current_session_context_class = jta, à la place de thread ?

    Effectivement, avec les sessions contextuelles, le close est réalisé pour toi.

    Tu utilises une couche de service au dessus de tes DAOs ?
    Celle-ci est-elle bien configurée ?

    Montre le reste de ton DAO et ton fichier de conf spring.

    Ensuite, ton pool n'est à priori pas bien configuré pour un environnement de production, mais tu n'en es surement pas encore là.

  6. #6
    Membre à l'essai
    Profil pro
    Inscrit en
    Août 2007
    Messages
    48
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Août 2007
    Messages : 48
    Points : 16
    Points
    16
    Par défaut
    Merci de ta réponse

    Si tu veux calquer tes sessions sur tes transactions, ne devrais-tu pas utiliser hibernate.current_session_context_class = jta, à la place de thread ?
    Effectivement, je vois ca dans la doc HIbernate 3.1 chapitre 2.5, c'est JTA qui doit etre utilisé a la place de thread a partir de hibernate 3.0.1.
    Je ne pense pas avoir besoin spécifiquement de sessions contextuelles. j'enlève donc cette config (qui me reste d'un tutoriel).

    Tu utilises une couche de service au dessus de tes DAOs ?
    Celle-ci est-elle bien configurée ?
    Montre le reste de ton DAO et ton fichier de conf spring.
    oui j'utilise une couche de service que j'instancie avec Spring

    applicationContext.xml
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
     
    	<bean id="actionService" class="fr.vivreaction.business.ActionService" >
        	<property name="actionDao" ref="actionDao"/>
    	</bean>
     
        <bean id="actionDao" class="fr.vivreaction.hibernate.dao.ActionDao">
         	<property name="sessionFactory" ref="sessionFactory" />
    	</bean>
    mon DAO extend HibernateDaoSupport, et implémente une interface.
    rien de plus

    Ensuite, ton pool n'est à priori pas bien configuré pour un environnement de production, mais tu n'en es surement pas encore là.
    J'en suis conscient, je suis en développement, et je reviendrai surement par ici le moment venu

  7. #7
    Expert confirmé
    Profil pro
    Inscrit en
    Août 2006
    Messages
    3 274
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 3 274
    Points : 4 141
    Points
    4 141
    Par défaut
    A mon avis ton actionService est mal configuré.
    Il n'y a pas de lien avec ton transactionProxy.

    Tu devrais avoir quelque chose du genre :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    <bean id="actionService" parent="transactionProxy">
    		<property name="target">
    			<bean class="cfr.vivreaction.business.ActionService"/>
    		</property>
    </bean>

    EDIT : sans oublier l'injection du DAO

  8. #8
    Membre à l'essai
    Profil pro
    Inscrit en
    Août 2007
    Messages
    48
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Août 2007
    Messages : 48
    Points : 16
    Points
    16
    Par défaut
    Citation Envoyé par fr1man Voir le message
    A mon avis ton actionService est mal configuré.
    Il n'y a pas de lien avec ton transactionProxy.

    Tu devrais avoir quelque chose du genre :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    <bean id="actionService" parent="transactionProxy">
    		<property name="target">
    			<bean class="cfr.vivreaction.business.ActionService"/>
    		</property>
    </bean>

    EDIT : sans oublier l'injection du DAO
    Merci

    j'ai donc maintenant
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    <bean id="actionService" parent="transactionProxy">
    		<property name="target">
    			<bean class="fr.vivreaction.business.ActionService">
        			<property name="actionDao" ref="actionDao"/>
    			</bean>
    		</property>
    	</bean>
    maintenant il me lance un "Session is closed"
    il doit me manquer la moitié de la configuration alors...

    Aurais-tu un lien pour un tuto (fr ou en) ou bien de la doc la dessus que je comprenne ce que je fais ?

    Merci encore

  9. #9
    Expert confirmé
    Profil pro
    Inscrit en
    Août 2006
    Messages
    3 274
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 3 274
    Points : 4 141
    Points
    4 141
    Par défaut
    Ton problème est-il entièrement résolu ?

    Regarde sur ce site s'il y a des tutos qui expliquent tout ça.
    Sinon, tu peux consulter les doc d'Hibernate et de Spring qui sont assez complète, mais c'est assez indigeste

  10. #10
    Membre à l'essai
    Profil pro
    Inscrit en
    Août 2007
    Messages
    48
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Août 2007
    Messages : 48
    Points : 16
    Points
    16
    Par défaut
    Mon problème n'est pas du tout résolu.
    Tous les tutos hibernate/Mysql que j'ai parcouru proposent une config relativement basique, ou ancienne (HibernateUtil, thread)

    je vais parcourir la doc, oui.

    je ne suis pas vraiment familier avec les configurations de bdd et d'accès aux bases. Tous les liens m'intéressent

    Merci de ton aide.

  11. #11
    Expert confirmé
    Profil pro
    Inscrit en
    Août 2006
    Messages
    3 274
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 3 274
    Points : 4 141
    Points
    4 141
    Par défaut
    Tu ne devrais pas être très loin de la solution.
    Il ne doit pas en falloir beaucoup plus pour que ça fonctionne.
    Là, tu as un "session is closed" quand tu appelles ton service ?

  12. #12
    Membre à l'essai
    Profil pro
    Inscrit en
    Août 2007
    Messages
    48
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Août 2007
    Messages : 48
    Points : 16
    Points
    16
    Par défaut
    oui, j'ai "session is closed" des le premier appel au service.

    Voila une piste :

    http://blog.m1key.me/2010/06/spring-...anagement.html

    mais ca ne résout pas mon problème :/ j'y croyais

    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
     
     
    	@SuppressWarnings("unchecked")
    	@Transactional(readOnly = true)
    	public List<Action> getListeActions() {
     
    		Session session = getSession();
    		try {
    			log.debug("getListeActions");
     
    			Query query = session.createQuery("from Action");
     
    			List<Action> result = new ArrayList<Action>();
     
    			result.addAll(query.list());
     
    			return result;
     
    		} catch (RuntimeException re) {
    			log.error("get failed", re);
    			throw re;
    		} finally {
    			session.close();
    		}
    	}

  13. #13
    Expert confirmé
    Profil pro
    Inscrit en
    Août 2006
    Messages
    3 274
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 3 274
    Points : 4 141
    Points
    4 141
    Par défaut
    Là tu es en train de mélanger configuration XML et annotations.
    Ta méthode est déjà transactionnelle, pas besoin de rajouter l'annotation.

    Vire le close, tu n'en as pas besoin.

  14. #14
    Membre à l'essai
    Profil pro
    Inscrit en
    Août 2007
    Messages
    48
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Août 2007
    Messages : 48
    Points : 16
    Points
    16
    Par défaut
    ok

    j'ai viré les close() et les annotations,
    je retombe sur :

    Data source rejected establishment of connection, message from server: "Too many connections"


    :'(

  15. #15
    Expert confirmé
    Profil pro
    Inscrit en
    Août 2006
    Messages
    3 274
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 3 274
    Points : 4 141
    Points
    4 141
    Par défaut
    Tu as peut-être des connexions non fermées.
    Essaie de réinitialiser ton pool de connexions ou redémarre ton serveur.

  16. #16
    Membre à l'essai
    Profil pro
    Inscrit en
    Août 2007
    Messages
    48
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Août 2007
    Messages : 48
    Points : 16
    Points
    16
    Par défaut
    a chaque erreur "Too many connexion" ou "Session is closed" je suis obligé de redémarrer Tomcat.
    "Too many connexion" apparait évidemment en multipliant les appels, quand j'atteins le "max_connections" de mysql

    Qu'est ce que tu entends par "réinitialiser ton pool de connexion" ?

  17. #17
    Expert confirmé
    Profil pro
    Inscrit en
    Août 2006
    Messages
    3 274
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 3 274
    Points : 4 141
    Points
    4 141
    Par défaut
    Ok, donc te voilà revenu au point de départ.
    Peux-tu redonner tes fichiers de conf hibernate, spring, depuis les modifs ?

  18. #18
    Membre à l'essai
    Profil pro
    Inscrit en
    Août 2007
    Messages
    48
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Août 2007
    Messages : 48
    Points : 16
    Points
    16
    Par défaut
    hibernate.cfg.xml
    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
    <?xml version='1.0' encoding='utf-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
     
    <hibernate-configuration>
        <session-factory>
            <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mydb</property>
            <property name="hibernate.connection.username">root</property>
            <property name="hibernate.connection.password">root</property>
            <property name="hibernate.connection.pool_size">20</property>
            <!-- 
            <property name="hibernate.connection.release_mode">auto</property>
             -->
     
            <property name="show_sql">false</property>
            <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
     
            <property name="hibernate.c3p0.min_size">5</property>
            <property name="hibernate.c3p0.max_size">20</property>
            <property name="hibernate.c3p0.timeout">300</property>
            <property name="hibernate.c3p0.max_statement">0</property>
     
     
            <property name="hibernate.cache.use_query_cache">false</property>
            <property name="hibernate.cache.use_second_level_cache">false</property>
     
            <!-- Mapping files -->
            <mapping resource="fr/vivreaction/hibernate/Action.hbm.xml" />
               ...
     
        </session-factory>
    </hibernate-configuration>
    applicationContext.xml
    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
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
     
    <beans>
     
        <!-- sessionFactory  -->
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" destroy-method="close">
            <property name="configLocation">
                <value>classpath:hibernate.cfg.xml</value>
            </property>
        </bean>
        <!-- 
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
         -->
        <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"/>
        </bean>
        <bean id="transactionProxy" abstract="true"
            class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
            <property name="transactionManager" ref="transactionManager"/>
            <property name="transactionAttributes">
                <props>
                    <prop key="insert*">PROPAGATION_REQUIRED</prop>
                    <prop key="update*">PROPAGATION_REQUIRED</prop>
                    <prop key="save*">PROPAGATION_REQUIRED</prop>
                    <prop key="*">PROPAGATION_REQUIRED, readOnly</prop>
                </props>
            </property>
            <!-- 
             -->
        </bean>
     
     
        <!--**********************************
             BEANS Service    
        ***********************************-->
     
     
        <bean id="actionService" parent="transactionProxy">
            <property name="target">
                <bean class="fr.vivreaction.business.ActionService">
                    <property name="actionDao" ref="actionDao"/>
                </bean>
            </property>
        </bean>
         ...
     
     
        <!--**********************************
             BEANS Dao    
        ***********************************-->
     
        <bean id="actionDao" class="fr.vivreaction.hibernate.dao.ActionDao">
             <property name="sessionFactory" ref="sessionFactory" />
        </bean>
         ...
     
    </beans>

  19. #19
    Expert confirmé
    Profil pro
    Inscrit en
    Août 2006
    Messages
    3 274
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 3 274
    Points : 4 141
    Points
    4 141
    Par défaut
    Pour Mysql, il faut utiliser le moteur InnoDB pour avoir accès aux transactions.
    Il faut donc paramétrer le bon dialect : MySQLInnoDBDialect

  20. #20
    Membre à l'essai
    Profil pro
    Inscrit en
    Août 2007
    Messages
    48
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Août 2007
    Messages : 48
    Points : 16
    Points
    16
    Par défaut
    Effectivement, je corrige ca

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
            <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
    Mais ca ne corrige pas mon erreur ...

+ Répondre à la discussion
Cette discussion est résolue.
Page 1 sur 2 12 DernièreDernière

Discussions similaires

  1. Probleme de recuperation d'une liste avec Hibernate et Spring
    Par fabiolerusse dans le forum Hibernate
    Réponses: 4
    Dernier message: 22/05/2008, 15h04
  2. GWT <--> Mysql avec Hibernate
    Par tatemilio2 dans le forum GWT et Vaadin
    Réponses: 12
    Dernier message: 23/04/2008, 18h04
  3. [Data] Problème de lazy avec hibernate et Spring
    Par Invité dans le forum Spring
    Réponses: 3
    Dernier message: 20/02/2008, 20h03
  4. Réponses: 2
    Dernier message: 29/01/2007, 15h13
  5. Configurer MYSQL++ avec Dev-C++ pour une connexion à BDMySQL
    Par limouna dans le forum Installation
    Réponses: 1
    Dernier message: 24/07/2005, 21h25

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