manage datasource via maven profile
in production, tomee needs now to have its own datasource configuration
Showing
3 changed files
with
98 additions
and
23 deletions
... | @@ -31,15 +31,21 @@ | ... | @@ -31,15 +31,21 @@ |
31 | <!-- paths --> | 31 | <!-- paths --> |
32 | <custom.web.dir>src/main/java/org/legrog/web</custom.web.dir> | 32 | <custom.web.dir>src/main/java/org/legrog/web</custom.web.dir> |
33 | 33 | ||
34 | - <!-- misc --> | 34 | + <!-- misc tomee conf overriden by maven profiles --> |
35 | <debug.jvm.args/> | 35 | <debug.jvm.args/> |
36 | <tomee.autoreload/> | 36 | <tomee.autoreload/> |
37 | + <deltaspike.project.stage/> | ||
38 | + <grogdatabase.jdbc.driver/> | ||
39 | + <grogdatabase.jdbc.url/> | ||
40 | + <grogdatabase.username/> | ||
41 | + <grogdatabase.password/> | ||
42 | + <driver.lib/> | ||
37 | 43 | ||
38 | </properties> | 44 | </properties> |
39 | 45 | ||
40 | <profiles> | 46 | <profiles> |
41 | <profile> | 47 | <profile> |
42 | - <id>debug</id> | 48 | + <id>Tomee-debug</id> |
43 | <!-- | 49 | <!-- |
44 | activate this one to be able to attach a remote debbuger on tomee | 50 | activate this one to be able to attach a remote debbuger on tomee |
45 | --> | 51 | --> |
... | @@ -49,7 +55,7 @@ | ... | @@ -49,7 +55,7 @@ |
49 | </profile> | 55 | </profile> |
50 | 56 | ||
51 | <profile> | 57 | <profile> |
52 | - <id>autoreload</id> | 58 | + <id>Tomee-autoreload</id> |
53 | <!-- | 59 | <!-- |
54 | activate this one for tomee to reload (takes times) every times it detect a synchro | 60 | activate this one for tomee to reload (takes times) every times it detect a synchro |
55 | reminder : you can always force a reload by typing reload in the console while tomee:run is active | 61 | reminder : you can always force a reload by typing reload in the console while tomee:run is active |
... | @@ -58,8 +64,44 @@ | ... | @@ -58,8 +64,44 @@ |
58 | <tomee.autoreload>true</tomee.autoreload> | 64 | <tomee.autoreload>true</tomee.autoreload> |
59 | </properties> | 65 | </properties> |
60 | </profile> | 66 | </profile> |
61 | - </profiles> | ||
62 | 67 | ||
68 | + <profile> | ||
69 | + <id>DS-h2-embeded</id> | ||
70 | + <!-- | ||
71 | + Set the datasource to a user-defined H2 database and activate the H2 console servlet | ||
72 | + --> | ||
73 | + <properties> | ||
74 | + <deltaspike.project.stage>-Dorg.apache.deltaspike.ProjectStage=IntegrationTest | ||
75 | + </deltaspike.project.stage> | ||
76 | + <driver.lib>com.h2database:h2:1.4.193</driver.lib> | ||
77 | + | ||
78 | + <grogdatabase.jdbc.driver>org.h2.Driver</grogdatabase.jdbc.driver> | ||
79 | + <grogdatabase.jdbc.url>jdbc:h2:mem:grogdev</grogdatabase.jdbc.url> | ||
80 | + <grogdatabase.username>sa</grogdatabase.username> | ||
81 | + </properties> | ||
82 | + | ||
83 | + </profile> | ||
84 | + | ||
85 | + | ||
86 | + <profile> | ||
87 | + <id>DS-mysql-local</id> | ||
88 | + <!-- | ||
89 | + Set the datasource to the local mysql server | ||
90 | + --> | ||
91 | + <properties> | ||
92 | + <deltaspike.project.stage>-Dorg.apache.deltaspike.ProjectStage=Staging</deltaspike.project.stage> | ||
93 | + <driver.lib>mysql:mysql-connector-java:5.1.32</driver.lib> | ||
94 | + <grogdatabase.jdbc.driver>com.mysql.jdbc.Driver</grogdatabase.jdbc.driver> | ||
95 | + | ||
96 | + <!-- EDIT THE FOLLOWING PARAMETERS TO MATCH YOUR SERVER --> | ||
97 | + <grogdatabase.username>grogdev</grogdatabase.username> | ||
98 | + <grogdatabase.password>grogdev</grogdatabase.password> | ||
99 | + <!-- format is jdbc:mysql://HOST:PORT/SCHEMA?useSSL=true/false --> | ||
100 | + <grogdatabase.jdbc.url>jdbc:mysql://localhost/grogdev?useSSL=false</grogdatabase.jdbc.url> | ||
101 | + </properties> | ||
102 | + | ||
103 | + </profile> | ||
104 | + </profiles> | ||
63 | 105 | ||
64 | <dependencies> | 106 | <dependencies> |
65 | 107 | ||
... | @@ -83,6 +125,22 @@ | ... | @@ -83,6 +125,22 @@ |
83 | <artifactId>javaee-api</artifactId> | 125 | <artifactId>javaee-api</artifactId> |
84 | </dependency> | 126 | </dependency> |
85 | 127 | ||
128 | + <dependency> | ||
129 | + <groupId>org.apache.deltaspike.core</groupId> | ||
130 | + <artifactId>deltaspike-core-api</artifactId> | ||
131 | + </dependency> | ||
132 | + | ||
133 | + <dependency> | ||
134 | + <groupId>org.apache.deltaspike.core</groupId> | ||
135 | + <artifactId>deltaspike-core-impl</artifactId> | ||
136 | + <scope>runtime</scope> | ||
137 | + </dependency> | ||
138 | + | ||
139 | + <dependency> | ||
140 | + <groupId>org.springframework.boot</groupId> | ||
141 | + <artifactId>spring-boot-autoconfigure</artifactId> | ||
142 | + </dependency> | ||
143 | + | ||
86 | <!-- https://mvnrepository.com/artifact/org.omnifaces/omnifaces --> | 144 | <!-- https://mvnrepository.com/artifact/org.omnifaces/omnifaces --> |
87 | <dependency> | 145 | <dependency> |
88 | <groupId>org.omnifaces</groupId> | 146 | <groupId>org.omnifaces</groupId> |
... | @@ -154,9 +212,6 @@ | ... | @@ -154,9 +212,6 @@ |
154 | <dependency> | 212 | <dependency> |
155 | <groupId>mysql</groupId> | 213 | <groupId>mysql</groupId> |
156 | <artifactId>mysql-connector-java</artifactId> | 214 | <artifactId>mysql-connector-java</artifactId> |
157 | - <!-- | ||
158 | - <version>5.1.6</version> | ||
159 | - --> | ||
160 | </dependency> | 215 | </dependency> |
161 | 216 | ||
162 | </dependencies> | 217 | </dependencies> |
... | @@ -197,11 +252,20 @@ | ... | @@ -197,11 +252,20 @@ |
197 | <groupId>org.apache.tomee.maven</groupId> | 252 | <groupId>org.apache.tomee.maven</groupId> |
198 | <artifactId>tomee-maven-plugin</artifactId> | 253 | <artifactId>tomee-maven-plugin</artifactId> |
199 | <version>7.0.1</version> | 254 | <version>7.0.1</version> |
255 | + <executions> | ||
256 | + <execution> | ||
257 | + <phase>pre-site</phase> | ||
258 | + </execution> | ||
259 | + </executions> | ||
200 | <configuration> | 260 | <configuration> |
201 | <context>ROOT</context> | 261 | <context>ROOT</context> |
202 | 262 | ||
203 | <!-- debug agent to attach a remote debbuger, activate profile for that --> | 263 | <!-- debug agent to attach a remote debbuger, activate profile for that --> |
204 | - <args>${debug.jvm.args}</args> | 264 | + <args>${debug.jvm.args} ${deltaspike.project.stage}</args> |
265 | + | ||
266 | + <libs> | ||
267 | + <lib>${driver.lib}</lib> | ||
268 | + </libs> | ||
205 | 269 | ||
206 | <systemVariables> | 270 | <systemVariables> |
207 | <!-- | 271 | <!-- |
... | @@ -209,6 +273,16 @@ | ... | @@ -209,6 +273,16 @@ |
209 | --> | 273 | --> |
210 | <openejb.system.apps>true</openejb.system.apps> | 274 | <openejb.system.apps>true</openejb.system.apps> |
211 | <tomee.serialization.class.blacklist>-</tomee.serialization.class.blacklist> | 275 | <tomee.serialization.class.blacklist>-</tomee.serialization.class.blacklist> |
276 | + | ||
277 | + <GrogDatabase>new://Resource?type=DataSource</GrogDatabase> | ||
278 | + <GrogDatabase.JtaManaged>true</GrogDatabase.JtaManaged> | ||
279 | + | ||
280 | + <GrogDatabase.JdbcDriver>${grogdatabase.jdbc.driver}</GrogDatabase.JdbcDriver> | ||
281 | + <GrogDatabase.JdbcUrl>${grogdatabase.jdbc.url}</GrogDatabase.JdbcUrl> | ||
282 | + <GrogDatabase.UserName>${grogdatabase.username}</GrogDatabase.UserName> | ||
283 | + <GrogDatabase.Password>${grogdatabase.password}</GrogDatabase.Password> | ||
284 | + | ||
285 | + | ||
212 | </systemVariables> | 286 | </systemVariables> |
213 | 287 | ||
214 | 288 | ... | ... |
1 | package org.legrog.configuration; | 1 | package org.legrog.configuration; |
2 | 2 | ||
3 | import javax.enterprise.context.RequestScoped; | 3 | import javax.enterprise.context.RequestScoped; |
4 | +import javax.enterprise.inject.Disposes; | ||
4 | import javax.enterprise.inject.Produces; | 5 | import javax.enterprise.inject.Produces; |
5 | import javax.persistence.EntityManager; | 6 | import javax.persistence.EntityManager; |
6 | import javax.persistence.PersistenceContext; | 7 | import javax.persistence.PersistenceContext; |
7 | 8 | ||
8 | /** | 9 | /** |
9 | * JPA configuration class, used by all classes extending JpaRepository | 10 | * JPA configuration class, used by all classes extending JpaRepository |
11 | + * | ||
12 | + * No difference made between Integration and Production yet, | ||
13 | + * as there is only one PU in the environnement at a given moment | ||
10 | */ | 14 | */ |
11 | -public class JpaConfiguration { | 15 | +//@Exclude(ifProjectStage = ProjectStage.IntegrationTest.class) |
16 | +public class EntityManagerProducer { | ||
12 | 17 | ||
13 | /** | 18 | /** |
14 | * Injectable interface for persistence handling | 19 | * Injectable interface for persistence handling |
15 | */ | 20 | */ |
16 | @Produces | 21 | @Produces |
17 | @RequestScoped | 22 | @RequestScoped |
18 | - @PersistenceContext(unitName = "development-pu") | 23 | + @PersistenceContext(unitName = "Grog-DB") |
19 | public EntityManager entityManager; | 24 | public EntityManager entityManager; |
20 | 25 | ||
26 | + | ||
27 | + public void closeEntityManager(@Disposes EntityManager entityManager) { | ||
28 | + if (entityManager.isOpen()) { | ||
29 | + entityManager.close(); | ||
30 | + } | ||
31 | + | ||
32 | + } | ||
33 | + | ||
21 | } | 34 | } | ... | ... |
1 | <?xml version="1.0"?> | 1 | <?xml version="1.0"?> |
2 | <tomee> | 2 | <tomee> |
3 | - <Resource id="H2Database" type="javax.sql.DataSource"> | 3 | + <!-- Placeholder file if some resources are needed some day --> |
4 | - JdbcDriver = org.h2.Driver | ||
5 | - JdbcUrl = jdbc:h2:~/grog-dev.db | ||
6 | - UserName = sa | ||
7 | - JtaManaged= true | ||
8 | - </Resource> | ||
9 | - <!--<Resource id="migrationDatabase" type="javax.sql.DataSource">--> | ||
10 | - <!--JdbcDriver = com.mysql.jdbc.Driver--> | ||
11 | - <!--JdbcUrl = jdbc:mysql://localhost/migration?useSSL=false--> | ||
12 | - <!--UserName = grogdev--> | ||
13 | - <!--password = grogdev--> | ||
14 | - <!--JtaManaged= true--> | ||
15 | - <!--</Resource>--> | ||
16 | </tomee> | 4 | </tomee> |
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or login to post a comment