JBoss Community

MySQL module, datasource and persistence.xml (for Hibernate) configuration bash script

modified by Ondrej Zizka in JBoss AS 7 Development - View the full document

This is a bash script which will:

 

Ask you for AS 7 directory,

download MySQL JDBC driver,

create a module for it,

create myapp-ds.xml file using this module as a driver,

create persistence.xml file using that datasource.

 

What you need is to put the two files into your application's jar's  META-INF/  and change the datasource's connection-url, user-name and password.

And then to add a driver to standalone.xml:

 

        <driver name="mysql" module="com.mysql.jdbc">
                <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
        </driver>

 

The script to save and then run:

 

if [ "$1" == "" ] ; then
  echo "  Usage:  $0 <AS7 dir>"
  exit 1
fi

AS_DIR=$1


mkdir -p $AS_DIR/modules/com/mysql/jdbc/main
cd $AS_DIR/modules/com/mysql/jdbc/main

wget http://cdn.mysql.com/Downloads/Connector-J/mysql-connector-java-5.1.21.zip
unzip mysql-connector-java-5.1.21.zip
rm mysql-connector-java-5.1.21.zip
cp mysql-connector-java-5.1.21/mysql-connector-java-5.1.21-bin.jar .
rm -rf mysql-connector-java-5.1.21/


cat <<EOF > module.xml
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.0" name="com.mysql.jdbc">
    <resources>
        <resource-root path="mysql-connector-java-5.1.21-bin.jar"/>
    </resources>
    <dependencies>
        <module name="javax.api"/>
    </dependencies>
</module>
EOF

cd -

cat <<EOF > myapp-ds.xml
<?xml version="1.0" encoding="UTF-8"?>
<datasources xmlns="http://www.jboss.org/ironjacamar/schema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.jboss.org/ironjacamar/schema http://docs.jboss.org/ironjacamar/schema/datasources_1_1.xsd">

    <!-- JNDI at this location. We reference this in META-INF/persistence.xml -->
    <datasource jndi-name="java:jboss/datasources/MyAppDS"
            pool-name="myapp-ds-pool" enabled="true" use-java-context="true">
        <connection-url>jdbc:mysql://localhost:3306/myapp?characterEncoding=UTF-8&amp;characterSetResults=UTF-8&amp;autoReconnect=true&amp;zeroDateTimeBehavior=convertToNull</connection-url>
        <driver>mysql</driver>
        <security>
            <user-name>myapp</user-name>
            <password>myapp</password>
        </security>
    </datasource>

    <!-- JBAS010411: <drivers/> in standalone -ds.xml deployments aren't supported.
            ==> Needs to go to standalone.xml.
    <drivers>
        <driver name="mysql" module="com.mysql.jdbc">
                <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
        </driver>
    </drivers>
    -->
</datasources>
EOF

cat <<EOF > persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

    <persistence-unit name="MyAppPU" transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>java:jboss/datasources/MyAppDS</jta-data-source>  <!-- See <datasource jndi-name="..."> . -->
        <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>

        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
            <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
            <property name="hibernate.show_sql" value="false"></property>
            <property name="hibernate.cache.use_second_level_cache" value="true"/>
            <property name="hibernate.cache.use_query_cache" value="true" />
            <property name="hibernate.cache.use_minimal_puts" value="true"/>
        </properties>
    </persistence-unit>

</persistence>
EOF

Comment by going to Community

Create a new document in JBoss AS 7 Development at Community