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

Hibernate Java Discussion :

Hibernate & performance


Sujet :

Hibernate Java

  1. #21
    Membre confirmé
    Profil pro
    Sr. Software Engineer
    Inscrit en
    Août 2007
    Messages
    169
    Détails du profil
    Informations personnelles :
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Sr. Software Engineer

    Informations forums :
    Inscription : Août 2007
    Messages : 169
    Par défaut
    merci pour le lien fr1man et tes propositions.
    dis tchize_ est ce que le problème ne viendrait pas de:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    <property name="current_session_context_class">thread</property>
    Je suis autant surpris que toi par la fermeture des sessions et l'obligation de definir des transaction.
    Je vais regarder tout de près. En fait dans le projet sur lequel je bosse je suis chargé d'implementer la couche d'accès au données pour des developpeurs d'IHM qui ne doivent pas savoir que l'implemenetation est celle d'hibernate.
    Je le dis au cas où vous auriez des propositions à me faire pour abstraire au maximum hibernate et controlé les temps de réponses.

    J'utilise hiberante 3.2.5.ga
    Merci

  2. #22
    Membre Expert
    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
    Par défaut
    Tu as tout compris. La durée de vie de ta session est associée à celle du thread courant, donc quand il se termine, la session se termine également.

  3. #23
    Expert éminent
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Par défaut
    on peux voir ton code? on travaille à l'aveugle là. Le session_context précise comment se comportent SessionFactory.openSession() et SessionFactory.getCurrentSession().

  4. #24
    Membre confirmé
    Profil pro
    Sr. Software Engineer
    Inscrit en
    Août 2007
    Messages
    169
    Détails du profil
    Informations personnelles :
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Sr. Software Engineer

    Informations forums :
    Inscription : Août 2007
    Messages : 169
    Par défaut
    ci-dessous la version simplifiée des fichiers de mapping et mon code comme vous le souhaitiez:

    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
    
    <?xml version='1.0' encoding='utf-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    
    <hibernate-configuration>
        <session-factory >
    
    	<!-- local connection properties -->
    	<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/MyDataBase</property>
    	<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
    	<property name="hibernate.connection.username">postgres</property>
    	<property name="hibernate.connection.password">postgres</property>
    	<!-- property name="hibernate.connection.pool_size"></property -->
    
    		<!-- dialect for PostgreSQL -->
            <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
           
            <property name="hibernate.show_sql">false</property>
            <property name="hibernate.use_outer_join">true</property>
            <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
            
            <property name="hibernate.jdbc.batch_size">30</property>
              
          <!-- je dois avoir un problème avec cette propriété--> 
          <property name="current_session_context_class">thread</property>
     
            <!-- insertion des fichiers de mapping ici-->
    
        </session-factory>
    </hibernate-configuration>
    Document.hbm
    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
     
    <class name="Document" table="document">
     
    	<id name="Id" type="integer">
    	 <column name="id" sql-type="int(4)"/>
    	 <generator class="increment"/>
    	</id>
     
    	<property name="Name"	type="string">
    	<column name="name" sql-type="char(50)" not-null="true"/>
    	</property>
     
    	<!-- association inverse doc 1:N Phrase-->
     
    	<set name="Sentences"  inverse="true" cascade="all-delete-orphan">
    	    <key column="doc_id"/>
    	    <one-to-many class="Sentence"/> 	 
    	</set>
     
             <!-- association  Corpus -->
    	<many-to-one name="Corpus" class="Corpus" column="corpus_id"/>
     
    </class>
    Sentence.hbm
    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
     
     <class name="Sentence" table="sentence">
     
    	<id name="Id" type="integer">
    	 <column name="id" sql-type="int(4)"/>
    	 <generator class="increment"/>
    	</id>
     
    	<property name="Content"	type="string">
    	<column name="content" sql-type="char(300)" not-null="true"/>
    	</property>
     
            <!-- association doc -->
    	<many-to-one name="Document" class="Document" column="doc_id" />
    </class>
    comme je souhaite masquer l'utilisation d'hibernate, j'ai définit deux interface:
    MySession.java(on y trouve tous les service de session org.hibernate.Session à qui le boulot sera délégué)
    MyTransaction.java ( on y trouve tous les service de session org.hibernate.Transaction à qui le boulot sera délégué)

    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
     
    public class MyTransactionImpl implements MyTransaction{
     
    	// transaction hibernate utilisée par délegation
            private Transaction hibernateTransaction;
     
    	public MyTransactionImpl() {
    		// TODO Auto-generated constructor stub
    	}
     
    	public MyTransactionImpl(Transaction hiberTransaction) {
     
    		this.hibernateTransaction=hiberTransaction;
     
    	}
     
    	public Transaction getHibernateTransaction() {
    		return hibernateTransaction;
    	}
     
    	public void setHibernateTransaction(Transaction hibernateTransaction) {
    		this.hibernateTransaction = hibernateTransaction;
    	}
     
     
    	@Override
    	public void begin() {
     
    	}
     
    	@Override
    	public void commit() {
    		try {
    			hibernateTransaction.commit();
     
    		} catch (HibernateException e) {
    			e.printStackTrace();
     
     
    		}
    	}
     
    	@Override
    	public boolean isActive() {
     
    		return false;
    	}
     
    	@Override
    	public void rollback() {
    		try {
    			hibernateTransaction.rollback();
    		} catch (HibernateException e) {
     
              e.printStackTrace();
     
     
    		}
     
    	}
     
    	@Override
    	public void setTimeout(int seconds) {
     
     
    	}
     
    	@Override
    	public boolean wasCommitted() {
     
    		try {
    			return this.hibernateTransaction.wasCommitted();
    		} catch (HibernateException e) {
                e.printStackTrace();
     
    		}
    		return false;
    	}
     
    	@Override
    	public boolean wasRolledBack() {
     
    		try {
    			return this.hibernateTransaction.wasRolledBack();
    		} catch (HibernateException e) {
                e.printStackTrace();
     
    		}
    		return false;
    	}
     
    }
    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
     
    public class MySessionImpl implements MySession {
     
    	// session hibernate utilisée par délegation
    	private Session hibernateSession;
     
     
    	/**
             * Instantiates a new session.
             */
    	public MySessionImpl() {
    		try {
     
    //creation d'un session hibernate à partir de hibernate.cfg.xml			    HibernateUtil.initialize(Constants.HIBERNATE_CONFIG_FILE_PATH);
     
    //initialisation grace à SessionFactory.getCurrentSession();
    this.hibernateSession = HibernateUtil.getCurrentSession();
     
     
    		} catch (HibernateException e) {
     
    			e.printStackTrace();
     
    		}
    	}
     
     
    	private Session getHibernateSession() {
    		return hibernateSession;
    	}
     
     
    	private void setHibernateSession(Session hibernateSession) {
    		this.hibernateSession = hibernateSession;
    	}
     
     
    	public MyTransaction beginTransaction()  {
     
    		try {
    			return new MyTransactionImpl(this.hibernateSession
    					.beginTransaction());
    		} catch (HibernateException e) {
     
    			e.printStackTrace();
     
     
    		}
     
    	}
     
     
    	@Override
    	public void close()  {
    		try {
    			this.hibernateSession.close();
    		} catch (HibernateException e) {
     
    			e.printStackTrace();
     
     
    		}
     
    	}
     
     
    	@Override
    	public void commitTransaction(MyTransaction tx) {
     
    		tx.commit();
    	}
     
     
    	@Override
    	public void executeQuery(String sqlQuery) {
    		// TODO Auto-generated method stub
     
    	}
     
     
    	@Override
    	public void clear() {
     
    		this.hibernateSession.clear();
     
    	}
     
     
    	@Override
    	public void flush() {
    		try {
    			this.hibernateSession.flush();
    		} catch (HibernateException e) {
     
    			e.printStackTrace();
     
     
    		}
    	}
    }
    HibernateUtil.java:
    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
     
    public class HibernateUtil {
     
     
    	private static SessionFactory sessionFactory;
    	private static Configuration config = new Configuration();
     
    	public static Configuration getConfig() {
    		return config;
    	}
     
    	public static SessionFactory getSessionFactory() {
    		return sessionFactory;
    	}
     
    	/**
             * Configure the session factory by reading hibernate config file
             * 
             * @param configFileName
             *            the name of the configuration file
             */
    	public static void initialize(String configFileName){
     
          buildSessionFactory(configFileName);
    	}
     
    	private static void buildSessionFactory(String configFileName) {
     
    		if (null == configFileName)
    			config.configure();
    		else {
    			config.configure(configFileName);
     
    			try {
    				// Create the SessionFactory from hibernate.cfg.xml
     
    				sessionFactory = config.buildSessionFactory();
     
    			} catch (Throwable ex) {
    				// Make sure you log the exception, as it might be swallowed
    				System.err.println("Initial SessionFactory creation failed."
    						+ ex);
    				throw new ExceptionInInitializerError(ex);
    			}
    		}
    	}
     
    	/**
             * Return a new Session object that must be closed when the work has been
             * completed.
             * 
             * @return the active Session
             */
    	public static Session getCurrentSession() throws HibernateException {
    		return getSessionFactory().getCurrentSession();
    	}
     
    }

    Voila en simplifié le code qui m'oblige à créer les transaction même pour des opération de select, et qui ensuite faire automatique ma session lors d'un commit. A mon avis il ya un truc par clair avec
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    <property name="current_session_context_class">thread</property>
    merci

  5. #25
    Membre Expert
    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
    Par défaut
    Ton application utilise-t-elle plusieurs threads ?

    Pour les transactions, tu ferais bien de prendre l'habitude de les déclarer, même pour de simples lectures (cf explication de deux membres de l'équipe Hibernate):
    https://forum.hibernate.org/viewtopi...098&highlight=

  6. #26
    Membre confirmé
    Profil pro
    Sr. Software Engineer
    Inscrit en
    Août 2007
    Messages
    169
    Détails du profil
    Informations personnelles :
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Sr. Software Engineer

    Informations forums :
    Inscription : Août 2007
    Messages : 169
    Par défaut
    J'accepte qu'il faille créer des transactions. ce que je ne comprend pas c'est pourquoi un transaction.commit() ferme ma session (session.close()).

    Je ne sais pas si c'est un bon choix mais j'opterais pour un multithread pour laisser une certaine marche de manoeuvre au deveppeur utilisant ma couche d'accès aux données.

  7. #27
    Membre Expert
    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
    Par défaut
    Je parlais de actuellement.
    Car si tu as ta session rattachée au thread courant (grâce à ta propriété <property name="current_session_context_class">thread</property> ), il est normal que la session se ferme à la fin de ton thread.

  8. #28
    Membre confirmé
    Profil pro
    Sr. Software Engineer
    Inscrit en
    Août 2007
    Messages
    169
    Détails du profil
    Informations personnelles :
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Sr. Software Engineer

    Informations forums :
    Inscription : Août 2007
    Messages : 169
    Par défaut
    j'utilise
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    <property name="current_session_context_class">thread</property>
    quand j'essaie avec
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    <property name="current_session_context_class">thread</property>
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    org.hibernate.HibernateException: No session currently bound to execution context
    	at org.hibernate.context.ManagedSessionContext.currentSession(ManagedSessionContext.java:50)
    	at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:544)
    ce serait quoi la bonne configuration?

  9. #29
    Expert éminent
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Par défaut
    tu peux nous montrer un exemple de code qui foire, histoire que l'on vois bien que la session a été fermée par le commit?

  10. #30
    Membre confirmé
    Profil pro
    Sr. Software Engineer
    Inscrit en
    Août 2007
    Messages
    169
    Détails du profil
    Informations personnelles :
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Sr. Software Engineer

    Informations forums :
    Inscription : Août 2007
    Messages : 169
    Par défaut getCurrentSession() vs openSession()
    Finalement le problème était lié à la façon de créer ma session hibernate.
    j'utilisais la méthode getCurrentSession() qui se base sur pattern Session per Transaction. Voila pourquoi chaque commit() fermait automatique la session courante.
    En utilisant openSession() le problème est résolu.

+ Répondre à la discussion
Cette discussion est résolue.
Page 2 sur 2 PremièrePremière 12

Discussions similaires

  1. Hibernate et performance CPU
    Par Sniper37 dans le forum Hibernate
    Réponses: 31
    Dernier message: 02/10/2009, 12h13
  2. Réponses: 8
    Dernier message: 21/11/2006, 11h54
  3. [Hibernate][Ibatis] Problème de performance..
    Par Saloucious dans le forum Hibernate
    Réponses: 2
    Dernier message: 29/10/2005, 13h21

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