Archivi tag: Jta

JBoss As 7 e HSQLDB

Rieccoci al lavoro anche se per un brevissimo periodo di tempo con Java, e le applicazioni J2EE..
Per un prototipo ho bisogno di un DB veloce e semplice da installare, magari residente in memoria.
Dopo qualche prova fallimentare con Derby e con H2 (causa mie lacune), decido di testare il famoso HyperSQL DB, HSQLDB.
http://hsqldb.org/

Dovendo utilizzare un datasource configurato tramite JNDI, e quindi rendere residente su Application Server il mio db, ho dovuto configurare in maniera opportuna JBOSS..

Per prima cosa dobbiamo inserire un “MODULE” dentro l’installazione di jboss dove andare a mettere il jar del database in questione.
quindi dentro la cartella Modules, nella cartella org, creiamo una cartella hsqldb e al suo interno una cartella main.

Se non sappiamo bene come fare o abbiamo paura di sbagliare, possiamo prendere spunto da come è configurato h2 su jboss, partendo dalla cartella com/h2database.

Dopo aver creato la cartella main al suo interno dobbiamo mettere il file hsqldb.jar (io sto usando la versione 2.2.8) che potete scaricare dal sito ufficiale, poi creiamo un file module.xml e mettiamo questo contenuto:

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
<?xml version="1.0" encoding="UTF-8"?>

<!--
 ~ JBoss, Home of Professional Open Source.
 ~ Copyright 2010, Red Hat, Inc., and individual contributors
 ~ as indicated by the @author tags. See the copyright.txt file in the
 ~ distribution for a full listing of individual contributors.
 ~
 ~ This is free software; you can redistribute it and/or modify it
 ~ under the terms of the GNU Lesser General Public License as
 ~ published by the Free Software Foundation; either version 2.1 of
 ~ the License, or (at your option) any later version.
 ~
 ~ This software is distributed in the hope that it will be useful,
 ~ but WITHOUT ANY WARRANTY; without even the implied warranty of
 ~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 ~ Lesser General Public License for more details.
 ~
 ~ You should have received a copy of the GNU Lesser General Public
 ~ License along with this software; if not, write to the Free
 ~ Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 ~ 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 -->

<module xmlns="urn:jboss:module:1.1" name="org.hsqldb">

    <resources>
        <resource-root path="hsqldb.jar"/>
    </resources>
    <dependencies>  
    <module name="javax.api"/>  
    <module name="javax.transaction.api"/>  
  </dependencies>  
</module>

Fatto questo apriamo la configurazione di jboss che andremo ad usare (nel mio caso la standalone, e quindi il file standalone.xml contenuto dentro la cartella jboss/standalone/configuration) e modifichiamo la parte inerente i datasources e i drivers:
dentro la voce profiles/subsystem ( ) troveremo un datasource di test per il db H2 e il suo relativo driver configurato. Aggiungiamo un nostro datasource per il nostro prototipo di applicazione e il driver hsqldb appena configurato nel module:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<subsystem xmlns="urn:jboss:domain:datasources:1.0">
            <datasources>            
                <datasource jta="true" jndi-name="java:/jboss/datasources/testAppDB" pool-name="TESTAPPDS" enabled="true" use-java-context="true" use-ccm="true">
                    <connection-url>jdbc:hsqldb:mem:testAppDB;hsqldb.write_delay=false;shutdown=true</connection-url>
                    <driver>hsqldb</driver>
                    <pool>
                        <prefill>false</prefill>
                        <use-strict-min>false</use-strict-min>
                        <flush-strategy>FailingConnectionOnly</flush-strategy>
                    </pool>
                    <security>
                        <user-name>sa</user-name>
                    </security>
                </datasource>
                <drivers>
                    <driver name="hsqldb" module="org.hsqldb"/>
                </drivers>
            </datasources>
        </subsystem>

Il gioco è fatto. Così facendo ora potremo reperire una connessione tramite lookup Jndi e ottenere così una connessione (jta compatibile) per la prototipazione delle nostre applicazioni.