Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

How to execute SQL statements during deployment

Excerpt

The article is about how

There may be times where you will want

to execute some cleanup or initialization SQL statements during the deployment time of, for example, a

datasource

data source.

Below is an example of how this can be done.

...

Code Block
xml
xml
<connector xmlns="http://geronimo.apache.org/xml/ns/j2ee/connector-${geronimoSchemaVersion}">
    <environment>
        <moduleId>
            <groupId>${pom.groupId}</groupId>
            <artifactId>${pom.artifactId}</artifactId>
            <version>${version}</version>
            <type>car</type>
        </moduleId>
        <dependencies>
            <dependency>
                <groupId>org.apache.geronimo.configs</groupId>
                <artifactId>system-database</artifactId>
                <type>car</type>
            </dependency>
            <!-- SQL files -->
            <dependency>
                <groupId>org.apache.geronimo.sample</groupId>
                <artifactId>my-sql</artifactId>
                <version>${monitoringConsoleVersion}</version>
                <type>jar</type>
            </dependency>
        </dependencies>
    </environment>

    <resourceadapter>
        <outbound-resourceadapter>
            <!-- DB Pool -->
            <connection-definition>
                <connectionfactory-interface>javax.sql.DataSource</connectionfactory-interface>
                <connectiondefinition-instance>
                    <name>jdbc/myDS</name>
                    <config-property-setting name="CreateDatabase">true</config-property-setting>
                    <config-property-setting name="DatabaseName">MyDB</config-property-setting>
                    <connectionmanager>
                        <local-transaction/>
                        <single-pool>
                            <max-size>10</max-size>
                            <min-size>0</min-size>
                            <match-one/>
                        </single-pool>
                    </connectionmanager>
                </connectiondefinition-instance>
            </connection-definition>
         </outbound-resourceadapter>
     </resourceadapter>

     <!-- These two GBeans will create the tables for the database automatically -->
     <gbean name="MyDSGBean" class="org.apache.geronimo.connector.wrapper.DatabaseInitializationGBean">
	 <!-- Execute the createTable.sql SQL statements if the following query does NOT return any records -->
         <attribute name="testSQL">SELECT t.tablename FROM SYS.SYSTABLES t WHERE lower(t.tablename)='myTable'</attribute>
	 !-- Path to the SQL file defined in the module: 'my-sql' where the dependency above is referencing -->
         <attribute name="path">META-INF/database/derby/createTables.sql</attribute>
         <reference name="DataSource">
             <name>jdbc/MyDS</name>
         </reference>
     </gbean>
 </connector>