Monday, July 28, 2014

EJB 3.1 and JTA

Configuring JTA and EJB 3.1 with JBOSS 7.1.1 final

  Please see the post on how to configure the different JTA data sources on JBOSS7


Bean managed JTA transactions

EJB 3.1 and JPA

Some fundamentals tips to start with JPA and ejb 3.1 with JBOSS 7.1.1 final

Add the data sources to the JBOSS_HOME/standalone/configuration/standalone.xml

<subsystem xmlns="urn:jboss:domain:datasources:1.0">
            <datasources>
                <datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">
                    <connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1</connection-url>
                    <driver>h2</driver>
                    <security>
                        <user-name>sa</user-name>
                        <password>sa</password>
                    </security>
                </datasource>
                <datasource jndi-name="java:jboss/datasources/MySqlDS" pool-name="MySqlDS" enabled="true" use-java-context="true">
                    <connection-url>jdbc:mysql://localhost:3306/nit_test</connection-url>
                    <driver>mysqlDriver</driver>
                    <security>
                        <user-name>root</user-name>
                        <password>root</password>
                    </security>
                </datasource>
                <drivers>
                    <driver name="h2" module="com.h2database.h2">
                        <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
                    </driver>
                    <driver name="mysqlDriver" module="com.mysql">
                        <driver-class>com.mysql.jdbc.Driver</driver-class>
                        <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
                    </driver>
                </drivers>
            </datasources>
        </subsystem>
       

Choose the right JDBC driver.
Some drivers do not support two phase commit. Hence a choice must be made for the right driver.
For example MySQL has DataSource classes like com.mysql.jdbc.jdbc2.optional.MysqlDataSource and com.mysql.jdbc.jdbc2.optional.MysqlXADataSource. Please chooce them wisely. For a list of JDBC Drivers, please refer to: http://docs.oracle.com/cd/E19798-01/821-1751/ghhvc/index.html - See more at: http://www.developerscrappad.com/435/java/java-ee/ejb-3-x-jpa-bean-managed-transaction-with-javax-ejb-usertransaction/#sthash.OPPE5fx7.dpuf
For example mysql datasources classes like com.mysql.jdbc.jdbc2.optional.MysqlDataSource
and com.mysql.jdbc.jdbc2.optional.MysqlXADataSource.
Check oracle documentation JDBC data sources

Add Connection jars to JBOSS modules

Create a directory in JBOSS_HOME/modules/com/mysql/main ( if not already present )
Add mysql-connector-java-x.x.x.jar to this directory.

Edit the module.xmla shown below:

<module xmlns="urn:jboss:module:1.1" name="com.mysql">
    <resources>
        <resource-root path="mysql-connector-java-5.1.31-bin.jar"/>
    </resources>

    <dependencies>
      <module name="javax.api"/>
    </dependencies>
</module>

Add persistence.xml to the EJB project

Add persistence.xml to the META-INF of the project.

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"
        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
        <persistence-unit name="Counter">
               <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <jta-data-source>java:jboss/datasources/MySqlDS</jta-data-source>
      <class>com.goraksh.test.JPACounterEntity</class>
      <exclude-unlisted-classes>true</exclude-unlisted-classes>
    
        </persistence-unit>
</persistence>

Here is the jta-data-source is the JNDI name of the mysql data source configured in the standalone.xml

Using managed JTA with EJB 3.1 with JBOSS 7.1.1

Managed JTA transactions

For example MySQL has DataSource classes like com.mysql.jdbc.jdbc2.optional.MysqlDataSource and com.mysql.jdbc.jdbc2.optional.MysqlXADataSource. Please chooce them wisely. For a list of JDBC Drivers, please refer to: http://docs.oracle.com/cd/E19798-01/821-1751/ghhvc/index.html - See more at: http://www.developerscrappad.com/435/java/java-ee/ejb-3-x-jpa-bean-managed-transaction-with-javax-ejb-usertransaction/#sthash.OPPE5fx7.dpuf