Bonjour à tous,
je voudrais mettre deux dataSources dans mon fichier de configuration Spring, je voudrais savoir si c'est possible si oui comment pourrais je le faire.
merci d'avance.
Version imprimable
Bonjour à tous,
je voudrais mettre deux dataSources dans mon fichier de configuration Spring, je voudrais savoir si c'est possible si oui comment pourrais je le faire.
merci d'avance.
suffit de declarer 2 dataSources avec des noms differents dans les fichiers de conf de spring.
David Gimelle
Developpeur J2ee
blog: Http://getj2ee.over-blog.com
et dans le sessionFactory je dois mettre mes deux data sources? pourrais tu me donner un exemple.
tu surement va devoir definir 2 sessionFactory.
Est-ce que tu peux montrer ton fichier de config ?
David Gimelle
Developpeur J2ee
blog: Http://getj2ee.over-blog.com
oui biensur:
Code:
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"?> <!-- Application context for AlimCpt testing --> <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" xsi:schemaLocation=" <a href="http://www.springframework.org/schema/beans" target="_blank">http://www.springframework.org/schema/beans</a> <a href="http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" target="_blank">http://www.springframework.org/schem...-beans-2.5.xsd</a> <a href="http://www.springframework.org/schema/tx" target="_blank">http://www.springframework.org/schema/tx</a> <a href="http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" target="_blank">http://www.springframework.org/schem...ing-tx-2.5.xsd</a> <a href="http://www.springframework.org/schema/aop" target="_blank">http://www.springframework.org/schema/aop</a> http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <bean id="anetoVueCotisationEncaissement" class="com.npfo.comptabilite.business.anetovuecotisationencaissement.AnetoVueCotisationEncaissementImpl"> <property name="anetoVueCotisationEncaissementDAO"> <ref bean="anetoVueCotisationEncaissementDAO" /> </property> </bean> <bean id="anetoVueCotisationEncaissementDAO" class="com.npfo.comptabilite.business.anetovuecotisationencaissement.AnetoVueCotisationEncaissementDAOImpl"> <property name="dataSource"> <ref bean="dataSource" /> </property> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@frtlr210.soltim:1521:ANETO51" /> <property name="username" value="I4UD17_USER" /> <property name="password" value="I4UD17_USER" /> </bean> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> </bean> <!-- enable the configuration of transactional behavior based on annotations --> <!--tx:annotation-driven transaction-manager="txManager"/--> </beans>
j'ai essayé ce que tu m'as dis et ça me donne toujours une exception, voici mon nouveau fichier de conf spring:
Code:
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 <?xml version="1.0" encoding="UTF-8"?> <!-- Application context for AlimCpt testing --> <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" xsi:schemaLocation=" <a href="http://www.springframework.org/schema/beans" target="_blank">http://www.springframework.org/schema/beans</a> <a href="http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" target="_blank">http://www.springframework.org/schem...-beans-2.5.xsd</a> <a href="http://www.springframework.org/schema/tx" target="_blank">http://www.springframework.org/schema/tx</a> <a href="http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" target="_blank">http://www.springframework.org/schem...ing-tx-2.5.xsd</a> <a href="http://www.springframework.org/schema/aop" target="_blank">http://www.springframework.org/schema/aop</a> http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <bean id="anetoVueCotisationEncaissement" class="com.npfo.comptabilite.business.anetovuecotisationencaissement.AnetoVueCotisationEncaissementImpl"> <property name="anetoVueCotisationEncaissementDAO"> <ref bean="anetoVueCotisationEncaissementDAO" /> </property> </bean> <bean id="anetoVueCotisationEncaissementDAO" class="com.npfo.comptabilite.business.anetovuecotisationencaissement.AnetoVueCotisationEncaissementDAOImpl"> <property name="dataSource"> <ref bean="dataSource" /> </property> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@frtlr210.soltim:1521:ANETO51" /> <property name="username" value="I4UD17_USER" /> <property name="password" value="I4UD17_USER" /> </bean> /******************AnetoVueComptablesCommun*****************/ <bean id="anetovuescomptablescommun" class="com.npfo.comptabilite.business.common.anetovuescomptablescommun.AnetoVuesComptablesCommunImpl"> <property name="AnetoVuesComptablesCommunDao"> <ref bean="AnetoVuesComptablesCommunDao" /> </property> </bean> <bean id="AnetoVuesComptablesCommunDao" class="com.npfo.comptabilite.business.common.anetovuescomptablescommun.AnetoVuesComptablesCommunDaoImpl"> <property name="dataSourceComCom"> <ref bean="dataSourceComCom" /> </property> </bean> <bean id="dataSourceComCom" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@frtlr210.soltim:1521:ANETO51" /> <property name="username" value="I4UD17_OWNER" /> <property name="password" value="I4UD173403" /> </bean> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSourceComCom"> <ref bean="dataSourceComCom" /> </property> <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> </bean> <!-- enable the configuration of transactional behavior based on annotations --> <!--tx:annotation-driven transaction-manager="txManager"/--> </beans>
2 beans avec le même id="sessionFactory"…
j'ai réctifié mon fichier de conf spring, et j'ai toujours la même exception
Code:
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"?> <!-- Application context for AlimCpt testing --> <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" xsi:schemaLocation=" <a href="http://www.springframework.org/schema/beans" target="_blank">http://www.springframework.org/schema/beans</a> <a href="http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" target="_blank">http://www.springframework.org/schem...-beans-2.5.xsd</a> <a href="http://www.springframework.org/schema/tx" target="_blank">http://www.springframework.org/schema/tx</a> <a href="http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" target="_blank">http://www.springframework.org/schem...ing-tx-2.5.xsd</a> <a href="http://www.springframework.org/schema/aop" target="_blank">http://www.springframework.org/schema/aop</a> http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <bean id="anetoVueCotisationEncaissement" class="com.npfo.comptabilite.business.anetovuecotisationencaissement.AnetoVueCotisationEncaissementImpl"> <property name="anetoVueCotisationEncaissementDAO"> <ref bean="anetoVueCotisationEncaissementDAO" /> </property> </bean> <bean id="anetoVueCotisationEncaissementDAO" class="com.npfo.comptabilite.business.anetovuecotisationencaissement.AnetoVueCotisationEncaissementDAOImpl"> <property name="dataSource"> <ref bean="dataSource" /> </property> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@frtlr210.soltim:1521:ANETO51" /> <property name="username" value="I4UD17_USER" /> <property name="password" value="I4UD17_USER" /> </bean> /******************AnetoVueComptablesCommun*****************/ <bean id="anetovuescomptablescommun" class="com.npfo.comptabilite.business.common.anetovuescomptablescommun.AnetoVuesComptablesCommunImpl"> <property name="AnetoVuesComptablesCommunDao"> <ref bean="AnetoVuesComptablesCommunDao" /> </property> </bean> <bean id="AnetoVuesComptablesCommunDao" class="com.npfo.comptabilite.business.common.anetovuescomptablescommun.AnetoVuesComptablesCommunDaoImpl"> <property name="dataSource"> <ref bean="dataSourceComCom" /> </property> </bean> <bean id="dataSourceComCom" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@frtlr210.soltim:1521:ANETO51" /> <property name="username" value="I4UD17_OWNER" /> <property name="password" value="I4UD173403" /> </bean> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> <property name="sessionFactory" ref="sessionFactoryCom" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> </bean> <bean id="sessionFactoryCom" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSourceComCom" /> </property> <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> </bean> <!-- enable the configuration of transactional behavior based on annotations --> <!--tx:annotation-driven transaction-manager="txManager"/--> </beans>
un HibernateTransactionManager avec 2 property sessionFactory ?
Il faut aussi 2 transactions managers differents. Et puis 2 Dao differents ou 2 methodes differentes dans un seul Dao avec 2 Transaction manager.
David Gimelle
Developpeur J2EE
blog: http://getj2ee.over-blog.com
je comprends pas, où est ce que je devrais mettre mes deux DAOs, pourrais tu me dire où est ce que je dois modifier dans mon fichier de configuration spring, merci beaucoup
Est-ce que tu peux nous montrer un bout de ta dao. Par exemple la methode qui te permet de persister un objet?
voici le corps de mon fichier de conf,
Code:
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 <?xml version="1.0" encoding="UTF-8"?> <!-- Application context for AlimCpt testing --> <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" xsi:schemaLocation=" <a href="http://www.springframework.org/schema/beans" target="_blank">http://www.springframework.org/schema/beans</a> <a href="http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" target="_blank">http://www.springframework.org/schem...-beans-2.5.xsd</a> <a href="http://www.springframework.org/schema/tx" target="_blank">http://www.springframework.org/schema/tx</a> <a href="http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" target="_blank">http://www.springframework.org/schem...ing-tx-2.5.xsd</a> <a href="http://www.springframework.org/schema/aop" target="_blank">http://www.springframework.org/schema/aop</a> http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <bean id="anetoVueCotisationEncaissement" class="com.npfo.comptabilite.business.anetovuecotisationencaissement.AnetoVueCotisationEncaissementImpl"> <property name="anetoVueCotisationEncaissementDAO"> <ref bean="anetoVueCotisationEncaissementDAO" /> </property> </bean> <bean id="anetoVueCotisationEncaissementDAO" class="com.npfo.comptabilite.business.anetovuecotisationencaissement.AnetoVueCotisationEncaissementDAOImpl"> <property name="dataSource"> <ref bean="dataSource" /> </property> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@frtlr210.soltim:1521:ANETO51" /> <property name="username" value="I4UD17_USER" /> <property name="password" value="I4UD17_USER" /> </bean> /******************AnetoVueComptablesCommun*****************/ <bean id="anetovuescomptablescommun" class="com.npfo.comptabilite.business.common.anetovuescomptablescommun.AnetoVuesComptablesCommunImpl"> <property name="AnetoVuesComptablesCommunDao"> <ref bean="AnetoVuesComptablesCommunDao" /> </property> </bean> <bean id="AnetoVuesComptablesCommunDao" class="com.npfo.comptabilite.business.common.anetovuescomptablescommun.AnetoVuesComptablesCommunDaoImpl"> <property name="dataSource"> <ref bean="dataSourceComCom" /> </property> </bean> <bean id="dataSourceComCom" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@frtlr210.soltim:1521:ANETO51" /> <property name="username" value="I4UD17_OWNER" /> <property name="password" value="I4UD173403" /> </bean> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> </bean> <bean id="txManagercom" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactoryCom" /> </bean> <bean id="sessionFactoryCom" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSourceComCom" /> </property> <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> </bean> <!-- enable the configuration of transactional behavior based on annotations --> <!--tx:annotation-driven transaction-manager="txManager"/--> </beans>
merci
Est-ce que tu peux nous montrer un bout du code Java de ta dao. Par exemple la methode qui te permet de persister un objet?
Si tu as deux bases de données il te faut en effet tout dupliquer : deux sessionFactory Hibernate, par exemple.
A priori chaque DAO devrait n'utiliser qu'une seule base de données, donc cela ne devrait pas te poser de problème.
Par contre, si tu veux deux instances d'une même classe DAO, qui utilisent chacune une des deux bases de données : il te faut alors déclarer deux fois ton bean Spring DAO.... La bonne nouvelle c'est qu'on peut faire de l'héritage de configuration Spring, ce qui fait que cela ne devrait pas être trop fastidieux.
Attention cependant :
- Je vois que tu bosses pour Soltim (sympa de poster ton login/mot de passe!), et chez Cegedim vous avez une méthode que je qualifierais de "très particulière" de traiter les DAOs. Bref, je ne te garanti pas que ça fonctionne avec le framework maison (pour info, je suis un ancien du CETIP, qui a fusionné avec vous pour faire Cegedim Activ)
- A mon avis ce que tu vas vraiment vouloir faire ce sont des transactions distribuées entre tes 2 bases de données : mauvaise nouvelle, cela va être compliqué... La meilleure manière de faire pour toi serait de faire des synonymes d'une base dans l'autre base : ainsi, au niveau du Java, tu ne verrais qu'une seule base de données, et uniquement des transactions locales. Cela résoudrait tes problèmes. Il y a beaucoup de sociétés qui interdisent cela, mais chez Cegedim ça devrait passer, à moins que les choses n'aient beaucoup changées depuis l'époque