[jboss-dev-forums] [JBoss AS 7 Development] - MySQL Datasource configuration script

Ondrej Zizka do-not-reply at jboss.com
Sun Jul 29 07:16:15 EDT 2012


Ondrej Zizka [https://community.jboss.org/people/ozizka] created the document:

"MySQL Datasource configuration script"

To view the document, visit: https://community.jboss.org/docs/DOC-19001

--------------------------------------------------------------
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/ .

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&characterSetResults=UTF-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull</connection-url>
        <driver>mysql</driver>
        <security>
            <user-name>myapp</user-name>
            <password>myapp</password>
        </security>
    </datasource>
    
    <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
[https://community.jboss.org/docs/DOC-19001]

Create a new document in JBoss AS 7 Development at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=102&containerType=14&container=2225]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-dev-forums/attachments/20120729/4e3c8a57/attachment.html 


More information about the jboss-dev-forums mailing list