<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">
<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>
                                <td>
                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="https://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">JBoss Community</a></h1>
                                                                </td>
                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px; -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
MySQL module, datasource and persistence.xml (for Hibernate) configuration bash script
</h3>
<span style="margin-bottom: 10px;">
modified by <a href="https://community.jboss.org/people/ozizka">Ondrej Zizka</a> in <i>JBoss AS 7 Development</i> - <a href="https://community.jboss.org/docs/DOC-19001">View the full document</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">
<div class="jive-rendered-content"><p>This is a bash script which will:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Ask you for AS 7 directory,</p><p>download MySQL JDBC driver,</p><p>create a module for it,</p><p>create myapp-ds.xml file using this module as a driver,</p><p>create persistence.xml file using that datasource.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>What you need is to put the two files into your application's jar's  META-INF/  and change the datasource's <span style="font-family: courier new,courier;">connection-url</span>,<span style="font-family: courier new,courier;"> user-name</span> and <span style="font-family: courier new,courier;">password</span>.</p><p>And then to add a driver to standalone.xml:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-xml">        <span class="jive-xml-tag"><driver name="mysql" module="com.mysql.jdbc"></span>
                <span class="jive-xml-tag"><xa-datasource-class></span>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource<span class="jive-xml-tag"></xa-datasource-class></span>
        <span class="jive-xml-tag"></driver></span>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>The script to save and then run:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code">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
</code></pre></div>
<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
<p style="margin: 0;">Comment by <a href="https://community.jboss.org/docs/DOC-19001">going to Community</a></p>
        <p style="margin: 0;">Create a new document in JBoss AS 7 Development at <a href="https://community.jboss.org/choose-container!input.jspa?contentType=102&containerType=14&container=2225">Community</a></p>
</div></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>