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

Spring Java Discussion :

Utilisation de deux datasources


Sujet :

Spring Java

  1. #1
    Candidat au Club
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2006
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Administration - Collectivité locale

    Informations forums :
    Inscription : Avril 2006
    Messages : 7
    Points : 4
    Points
    4
    Par défaut Utilisation de deux datasources
    Bonjour,

    je souhaite paramétrer deux datasources dans ma configuration spring mais je ne sais pas trop comment m'y prendre.
    Il s'agit d'un service web permettant l'accès à une première base de données, la deuxième base de données permet de sauvegarder l'historique des accès/modifications.
    J'utilise hibernate et deux base de données mysql différentes.

    voici mon fichier de config :
    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
     
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:aop="http://www.springframework.org/schema/aop"
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
     
    	<!-- ========================= DATABASE RELATED DEFINITIONS ========================= -->
    	<!-- Here we setup the dataSource bean. It will be used by the persistence framework below.
    		We're using a connection pool implementation by Apache. The jdbc settings are replaced
    		by the placeholder data found in the jdbc.properties file defined above.
     
    	-->
    	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    		destroy-method="close">
    		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
    		<property name="url" value="jdbc:mysql://127.0.0.1/${bdd1}" />
    		<property name="username" value="${username1}" />
    		<property name="password" value="${password1}" />
    	</bean>
     
    	<bean id="dataSourceLog" class="org.apache.commons.dbcp.BasicDataSource"
    		destroy-method="close">
    		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
    		<property name="url" value="jdbc:mysql://127.0.0.1/${bdd2}" />
    		<property name="username" value="${username2}" />
    		<property name="password" value="${password2}" />
    	</bean>
     
    	<bean id="persistenceUnitManager"
    		class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
    		<property name="persistenceXmlLocations">
    			<list>
    				 <value>classpath*:META-INF/persistence.xml</value>
    				  <value>classpath*:META-INF/persistence_log.xml</value>
    			</list>
    		</property>
    		<property name="dataSources">
    			<map>
    				<entry key="bdd1" value-ref="dataSource" />
    				<entry key="bdd2" value-ref="dataSourceLog" />
    			</map>
    		</property>
    		<!-- if no datasource is specified, use this one -->
    		<property name="defaultDataSource" ref="dataSource" />
    	</bean>
     
    	<bean id="abstractEMF"
    		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" abstract="true">
    		<property name="jpaVendorAdapter">
    			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    				<property name="database" value="MYSQL" />
    				<property name="showSql" value="true" />
    				<property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" />
    				<property name="generateDdl" value="false" />
     
    			</bean>
    		</property>
    		<property name="jpaPropertyMap">
    			<map>
    				<entry key="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"/>
    				<entry key="hibernate.cache.use_query_cache" value="false"/>
    				<entry key="hibernate.cache.use_second_level_cache" value="false"/>
    				<entry key="hibernate.archive.autodetection" value="class, hbm" />
    				<entry key="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
    			</map>
    		</property>
    	</bean>
     
    	<bean id="entityManagerFactory" parent="abstractEMF">
    		<property name="persistenceUnitManager" ref="persistenceUnitManager" />
    		<property name="persistenceUnitName" value="bdd1"/>
    	</bean>
     
    	<bean id="entityManagerFactoryLog" parent="abstractEMF">
    		<property name="persistenceUnitManager" ref="persistenceUnitManager" />
    		<property name="persistenceUnitName" value="bdd2"/>
    	</bean>
     
     
     
    	<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
     
    	<!-- post-processors for all standard config annotations -->
      	<context:annotation-config />
     
    	<!-- Exception translation bean post processor -->
     	<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
     
    	<tx:annotation-driven transaction-manager="transactionManager"/>
     
    	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    		<property name="entityManagerFactory" ref="entityManagerFactory" />
    	</bean>
     
    	<bean id="transactionManagerLog" class="org.springframework.orm.jpa.JpaTransactionManager">
    		<property name="entityManagerFactory" ref="entityManagerFactoryLog" />
    	</bean>
     
    </beans>
    Mes fichiers persistence sont du type:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    <persistence version="1.0"
    	xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
    	<persistence-unit name="bdd1" transaction-type="RESOURCE_LOCAL">
    		<class>com.bidule.projet.module1.model.AccessHistory</class>
                    ...
    		<exclude-unlisted-classes />
    	</persistence-unit>
    </persistence>
    le chargement du contexte s'effectue sans erreur par contre lors de l'exécution de l'application les requêtes ne semble pas s'exécuter (requete select ne retournant aucun résultat) alors que cela marchait très bien avant l'ajout de la config pour l'historique (deuxième datasources).

    Si quelqu'un a une idée d'où peut venir le problème.
    Merci d'avance

  2. #2
    Candidat au Club
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2006
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Administration - Collectivité locale

    Informations forums :
    Inscription : Avril 2006
    Messages : 7
    Points : 4
    Points
    4
    Par défaut
    Re,

    après quelques modifications, j'ai réussi à faire marcher une partie de l'application.
    Mais l'insertion dans l'historique ne marche toujours pas.
    J'obtiens l'information suivante:

    Thread-46] DEBUG o.h.e.def.AbstractSaveEventListener - delaying identity-insert due to no transaction in progress

    voici mon fichier de config :
    Code :

    Code XML : 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
     
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:aop="http://www.springframework.org/schema/aop"
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:jee="http://www.springframework.org/schema/jee"
    	xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
     
    	<!-- ========================= DATABASE RELATED DEFINITIONS ========================= -->
    	<!-- Here we setup the dataSource bean. It will be used by the persistence framework below.
    		We're using a connection pool implementation by Apache. The jdbc settings are replaced
    		by the placeholder data found in the jdbc.properties file defined above.
     
    	-->
    	<!-- JPA annotations bean post processor -->
    	<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor">
    	</bean>
     
    	<!-- Exception translation bean post processor -->
    	<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor">
    	</bean>
     
    	<context:annotation-config />
     
    	<tx:annotation-driven transaction-manager="transactionManagerPAP"/>
    	<tx:annotation-driven transaction-manager="transactionManagerLOG"/>
     
    	<bean id="entityManagerFactoryPAP" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    		<property name="persistenceUnitName" value="pap"/>
    		<property name="dataSource" ref="dataSourcePAP"/>
    		<property name="jpaVendorAdapter">
    			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    				<property name="database" value="MYSQL" />
    				<property name="showSql" value="true" />
    				<property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" />
    				<property name="generateDdl" value="false" />
     
    			</bean>
    		</property>
    		<property name="jpaPropertyMap">
    			<map>
    				<entry key="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"/>
    				<entry key="hibernate.cache.use_query_cache" value="false"/>
    				<entry key="hibernate.cache.use_second_level_cache" value="false"/>
    				<entry key="hibernate.archive.autodetection" value="class, hbm" />
    				<!--<entry key="hibernate.hbm2ddl.auto" value="create" />-->
    				<entry key="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
    			</map>
    		</property>
    	</bean>
     
    	<bean id="entityManagerFactoryLOG" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    		<property name="persistenceUnitName" value="log"/>
    		<property name="dataSource" ref="dataSourceLOG"/>
    		<property name="jpaVendorAdapter">
    			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    				<property name="database" value="MYSQL" />
    				<property name="showSql" value="true" />
    				<property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" />
    				<property name="generateDdl" value="false" />
     
    			</bean>
    		</property>
    		<property name="jpaPropertyMap">
    			<map>
    				<entry key="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"/>
    				<entry key="hibernate.cache.use_query_cache" value="false"/>
    				<entry key="hibernate.cache.use_second_level_cache" value="false"/>
    				<entry key="hibernate.archive.autodetection" value="class, hbm" />
    				<!--<entry key="hibernate.hbm2ddl.auto" value="create" />-->
    				<entry key="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
    			</map>
    		</property>
    	</bean>
     
    	<jee:jndi-lookup id="dataSourcePAP" jndi-name="jdbc/pap"/>
    	<jee:jndi-lookup id="dataSourceLOG" jndi-name="jdbc/log"/>
     
    	<bean id="transactionManagerPAP" class="org.springframework.orm.jpa.JpaTransactionManager">
    		<property name="entityManagerFactory" ref="entityManagerFactoryPAP"/>
    		<property name="dataSource" ref="dataSourcePAP"/>
    	</bean>
     
    	<bean id="transactionManagerLOG" class="org.springframework.orm.jpa.JpaTransactionManager">
    		<property name="entityManagerFactory" ref="entityManagerFactoryLOG"/>
    		<property name="dataSource" ref="dataSourceLOG"/>
    	</bean>
     
    </beans>


    Merci d'avance

  3. #3
    Expert confirmé
    Homme Profil pro
    Inscrit en
    Septembre 2006
    Messages
    2 938
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Septembre 2006
    Messages : 2 938
    Points : 4 359
    Points
    4 359
    Par défaut
    N datasources -> N transaction managers…

    comment avez-vous choisi de gérer vos transactions ?

    (FYI: @Transactional ne fonctionnera pas avec plusieurs EntityManager…)

Discussions similaires

  1. Datagrid et combobox avec deux datasource différents
    Par RaelRiaK dans le forum VB.NET
    Réponses: 9
    Dernier message: 07/02/2007, 09h20
  2. utilisation de deux jointures
    Par mealtone dans le forum Langage SQL
    Réponses: 16
    Dernier message: 15/08/2006, 13h53
  3. Réponses: 1
    Dernier message: 24/05/2006, 15h25
  4. Utilisation de deux versions de gcc sur la même plateforme
    Par Anouschka dans le forum Administration système
    Réponses: 5
    Dernier message: 04/05/2006, 11h32
  5. [CSS] Utilisation de deux div avec float
    Par Ditch dans le forum Mise en page CSS
    Réponses: 5
    Dernier message: 06/10/2005, 15h48

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