Dans l'ordre donc, j'ai bien-sûr téléchargé la dernière version d'H2 (1.1.105 datant du 19 décembre 2008).
Ensuite, j'ai lu la documentation O_o ! Et oui, ca aide toujours et on apprend moults choses : modes de compatibilités, performances, administration, limitations etc...
J'ai ensuite copié le jar "h2-1.1.105.jar" dans <project name>/thirdparty/jboss-5.0.0.GA/common/lib et dans <project name>/thirdparty/jboss-seam/lib pour la partie testNG avec JBoss Embedded.
Après ces quelques heures fructueuses qui m'ont confortées dans mon choix d'utiliser ce SGBD, j'ai créé mon fichier -ds.xml :
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!--
3 <!DOCTYPE datasources
4 PUBLIC "-//JBoss//DTD JBOSS JCA Config 1.5//EN"
5 "http://www.jboss.org/j2ee/dtd/jboss-ds_1_5.dtd">
6 -->
7 <!-- H2 database JBoss datasource -->
8 <datasources>
9 <local-tx-datasource>
10 <!-- The jndi name of the DataSource, it is prefixed with
11 java:/ -->
12 <!-- Datasources are not available outside the virtual
13 machine -->
14 <jndi-name>projectNameDatasource</jndi-name>
15
16 <!-- For in-process persistent db, saved when jboss
17 stops. -->
18 <connection-url>
19 jdbc:h2:file:${jboss.server.data.dir}${/}h2${/}projectNameDB;DB_CLOSE_ON_EXIT=FALSE
20 </connection-url>
21
22 <!-- The driver class -->
23 <driver-class>org.h2.Driver</driver-class>
24
25 <!-- The login and password -->
26 <user-name>sa</user-name>
27 <password/>
28
29 <transaction-isolation>TRANSACTION_READ_COMMITTED</transaction-isolation>
30
31 <!-- this will be run before a managed connection is removed
32 from the pool for use by a client-->
33 <check-valid-connection-sql>select 1 from DUAL</check-valid-connection-sql>
34
35 <!-- The minimum connections in a pool/sub-pool. Pools are
36 lazily constructed on first use -->
37 <min-pool-size>10</min-pool-size>
38
39 <!-- The maximum connections in a pool/sub-pool -->
40 <max-pool-size>20</max-pool-size>
41
42 <!-- The time before an unused connection is destroyed -->
43 <!-- NOTE: This is the check period. It will be destroyed
44 somewhere between 1x and 2x this timeout after last use -->
45 <idle-timeout-minutes>0</idle-timeout-minutes>
46
47 <!-- sql to call on an existing pooled connection when it is
48 obtained from pool -->
49 <check-valid-connection-sql>select 1 from DUAL</check-valid-connection-sql>
50
51 <!-- Whether to check all statements are closed when the
52 connection is returned to the pool, this is a debugging
53 feature that should be turned off in production -->
54 <track-statements/>
55
56 <!-- H2 DB benefits from prepared statement caching -->
57 <prepared-statement-cache-size>16</prepared-statement-cache-size>
58
59 </local-tx-datasource>
60
61 </datasources>
62
Vous noterez plusieurs choses :
- J'ai volontairement commenté la recherche de DTD pour éviter la connexion à www.jboss.org tout le temps.
- Dans l'URL de connexion (jdbc:h2:file:${jboss.server.data.dir}${/}h2${/}projectNameDB;DB_CLOSE_ON_EXIT=FALSE), j'ai placé la propriété DB_CLOSE_ON_EXIT=FALSE pour éviter des problèmes en cas d'arrêt de JBoss du style : org.h2.jdbc.JdbcSQLException: Database is already closed (to disable automatic closing at VM shutdown, add ";DB_CLOSE_ON_EXIT=FALSE" to the db URL)
- J'utilise pour le moment le compte SA (super administrateur) mais cela changera une fois en production.
Ceci fait, il faut passer à la configuration d'Hibernate. Ainsi le fichier /resources/META-INF/persistence.xml se trouve modifié comme suit :
1 <?xml version="1.0" encoding="UTF-8"?>
2 <persistence xmlns="http://java.sun.com/xml/ns/persistence"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation=
5 "http://java.sun.com/xml/ns/persistence
6 http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
7 version="1.0">
8 <persistence-unit name="projectNameDatabase">
9 <provider>org.hibernate.ejb.HibernatePersistence</provider>
10 <jta-data-source>java:/projectNameDatasource</jta-data-source>
11 <properties>
12 <!-- TODO: WHILE UNDER DEVELOPMENT, LET THIS PROPERTY TO
13 TODO: "create-drop"
14 -->
15 <!-- TODO: ONCE IN PRODUCTION, SET THIS PROPERTY TO "validate"
16 -->
17 <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
18 <property name="hibernate.show_sql" value="true"/>
19 <property name="hibernate.dialect"
20 value="org.hibernate.dialect.H2Dialect" />
21 <!-- These are the default for JBoss EJB3, but not for HEM: -->
22 <property name="hibernate.cache.provider_class"
23 value="org.hibernate.cache.HashtableCacheProvider"/>
24 <property name="hibernate.transaction.manager_lookup_class"
25 value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
26 </properties>
27 </persistence-unit>
28 </persistence>
29
Il faudra bien faire attention au dialect (ligne 14) et à la propriété hibernate.hbm2ddl.auto (ligne 12) (voir précédents posts).
Enfin comme indiqué dans la documentation d'H2, il faudra bien faire attention à un bug dans le dialect H2 (issue Jira HHH-3401 toujours ouverte à ce jour) présent donc en version 3.3.1.
Aucun commentaire:
Enregistrer un commentaire