Saturday, 6 July 2013

Spring Task Execution And Scheduling

If your applications needs a single or batch of tasks to be executed in an asynchronous manner at some point in future, then what you are ultimately looking for is TaskExecuter implementations and TaskScheduler of spring framework.
TaskExecuter allows us to execute tasks in an asynchronous manner
TaskScheduler allows us to schedule a task to run at some point in future.
To know more about taskexecuter and taskscheduler visit http://static.springsource.org/spring/docs/3.0.x/reference/scheduling.html

Here we can see how to declare taskexecuter and taskscheduler in spring configuration file
and how to create a bean with cron job @Scheduled annotation to schedule the date and time of execution.

Spring configuration file

Now to schedule a job for every 20 minutes

Make sure to replace xxxxxxxxxxx of <context:component-scan base-package="xxxxxxxxxxx" /> with the package name of your ScheduleService class.

Now, lets analyse the attributes of task executer and scheduler.


<task:scheduler id="taskScheduler" pool-size="1"/>

pool-size="1": Here the value "1" indicates that only one task can be scheduled at a time. If you want one or more task to be scheduled in an asynchronous manner, increase its value. For example, to schedule 5 tasks at a time ( <task:scheduler id="taskScheduler" pool-size="5"/> )

<task:executor id="taskExecutor" pool-size="1" queue-capacity="2"/>

As in the case of scheduler, pool-size with value "1" indicates that only one task can be executed at a time. If you set value 1 to pool-size of scheduler and executer, it behaves in a synchronous manner.ie, only one task can be scheduled and only one task can be executed at a time. If you want to execute more tasks at a time, just increase the value of the pool-size. For example, If you want to execute 5 tasks at a time  
<task:executor id="taskExecutor" pool-size="5" queue-capacity="2"/>

<task:executor id="taskExecutor" pool-size="1" queue-capacity="2"/>

Capacity of TaskExecuter queue. Before an item is pushed to the pool, it checks the current capacity of the pool. If the pool is completely filled and busy, it will be pushed to the queue, and it remains there until the TaskExecuter finish running a task. Here the value "2" defines that only 2 tasks can be put in to the queue before it moves on to the pool.


Tuesday, 2 July 2013

Avoid Duplicates in List

How to avoid duplicates in List 

There is no direct classes available in java to avoid duplicates. Use HashSet along with ArrayList to avoid duplicates

Monday, 1 July 2013

/proc/kcore and rinetd occupies the entire hard disk space

We had a strange problem in ubuntu server. This happened without any warning, still don't know whats the reason for that. I logged in to the system and couldn't do anything...I was getting strange errors, and then I saw the hard disk space, it was 0...0 space left. It was shocking for me. We have around 2TB of hard disk space, then I checked the folders one by one...there was one folder named "proc" that occupied almost the entire 1.32TB of data and the rest by the /var/log folder.
I googled many times, and the result I got is this "proc/kcore" is a virtual file, it will not occupy any space. Its protected and you cannot delete that file also, then I restarted the system. After a quck reboot, system was normal...everything was working fine, after an hour and so I checked the disk space again,  boooooooooommmmmm...almost 50% occupied by "proc/kcore" file again. I couldn't find an answer for this. then I upgraded ubuntu 10.04 to 12.x.
After a successfull upgrade everything seemed to be normal. After 3-4 days, I checked the space again..booooooooooomm...35% of space gone....I checked the "proc/kcore" file this time...Fortunately this time it's not his fault. then I checked the /var/logs folder....too many logs..
After searching for a very long time, I found that there is a process called "RINETD" that creates too many logs.

I still couldn't find an answer for my 1'st problem.

But I managed to solve issues with RINETD process. To disable the logs just perform the below steps.

1) Stop rinetd process first

       service rinetd stop

2) Delete the log files from logs folder

       cd var/logs
       rm -R *

3) Open rinetd config file and disable the log output

     nano /etc/rinetd.conf

     To disable log, comment the following

      # Remove the comment from the line below to enable logging option.
      # logfile /var/log/rinetd.log

4) Restart the service again

      service rinetd restart 

Saturday, 29 June 2013

Primefaces JSF Spring Web Application

Create java web application using primefaces

      I have been working with some of the web ui templates that makes my life easier for quite some time. I have tried vaadin, GWT kit, groovy and grails, primefaces etc. I would recommend Primefaces as the best option to design your template and pages. It's light weight compared to all other libraries. It has almost all UI components you are looking for. In my opinion, its the best ui component framework that works well with JSF and spring applications. Now it can be integrated with ROO applications also. primeface comes with built-in themes. If you want to apply twitter-bootstrap which is the most widely used themes in coming websites. Just add the maven dependency and enable the appropriate theme in web.xml. Its that simple.

Have a look at the comparison http://www.primefaces.org/whyprimefaces.html

Now we will see how to create a simple jsf-spring-prmeface maven web application.

Frameworks used:
  Spring 3.1.2.RELEASE
  JSF
  Spring security  3.1.0.RELEASE
  Primefaces

1) Since its a maven application, first get the required dependencies.

You can see that I have added bootstrap theme to my web application.

2) Now get the jsf dependencies. Most javaee containers supports built-in support for jsf. I have been working with glassfish server for quite some time. It has built in support for jsf. If you are using glassfish, add the below dependency.


servlet containers like tomcat needs the below dependencies

3) Add spring dependencies

If you want spring security, add the below dependencies too

4) Copy and paste the below contents to web.xml


5) Sample template jsf file
Clean and build your project. That's it!!!.


Thursday, 27 June 2013

Getting JDBC Connection from EntityManager ( EclipseLink )

when things gets too complex, your hibernate/eclipselink jpa implementations won't help. You may need to access the underlying JDBC connection. Logic to extract a JDBC connection from JTA environment and non-JTA environment is not the same.
If you are using JTA datasource managed by the container, then you can get the connection directly from the JavaEE container Datasource. Just inject or lookup the datasource from JNDI. As long as you are with in a JTA trasactional context, the JDBC connection will be the same connection used by JPA.
If you are using non-JTA datasource, then you can still access the datasource from the javaEE container if you want a new connection. If you want the same connection used by JPA, then you can get it from JPAEntityManager interface.


Getting JDBC connection from JTA Datasource:

  First take a look at persistence.xml
  

  Now inject the Datasource bean in your java file
  

Getting JDBC connection from NON-JTA Datasource:

  JPA 2.0

  JPA 1.0

Wednesday, 26 June 2013

Log4j Configuration

Steps to configure log4j in maven web application


Step: 1


   Add the required dependencies in pom.xml
                 

Step: 2

   Add log4j.xml in your application root folder.
   If you are using any IDE put log4j.xml in "src/main/resources". After compilation, it will be copied to "WEB-INF/classes" folder. Copy and paste the following contents in log4j.xml


I am using glassfish as the underlying server that's why the log folder relative path is "com.sun.aas.instanceRoot". For other servers, get the log folder path and replace that line. We are using RollingFileAppender, that enables us to create log files based on rolling policy. In other words, based on the date stamp it rolls at that time, 
            default is yyyy-MM-dd, (rolls at midnight)
See:http://logging.apache.org/log4j/companions/extras/apidocs/org/apache/log4j/rolling/TimeBasedRollingPolicy.html
So, instead of single file, It generates log files on daily basis.


Step: 3


    Add Log4jConfigListener in web.xml
    

To debug your java files
   
That's it! Now build and run the project.

Saturday, 22 June 2013

Spring RESOURCE_LOCAL and JTA configurations

Spring RESOURCE_LOCAL(Local Transactions) and JTA(Global Transactions) configurations

So often people tend to make many mistakes with RESOURCE_LOCAL and JTA configurations, Without knowing the actual usage of tags, developers often mix these two configuration related tags. By doing so, spring instantiate unwanted beans in memory. So, I thought this blow would helps to developers to fix or create a proper RESOURCE_LOCAL and JTA xml configurations. I will also explain how to create proper JTA production configuration. Yes, JTA can be configured in different ways. But for production, Its better to handle the connections by the application container itself. So before start creating these configurations one has to have a complete knowledge of the xml tags used in these configurations.I would suggest you to read and understand all the topics discussed in http://static.springsource.org/spring/docs/2.0.x/reference/transaction.html

Global Transactions. Global transactions have a significant downside, in that code needs to use JTA, and JTA is a cumbersome API to use (partly due to its exception model). Furthermore, a JTAUserTransaction normally needs to be sourced from JNDI: meaning that we need to use both JNDIand JTA to use JTA. Obviously all use of global transactions limits the reusability of application code, as JTA is normally only available in an application server environment. Previously, the preferred way to use global transactions was via EJB CMT (Container Managed Transaction): CMT is a form of declarative transaction management (as distinguished from programmatic transaction management). EJB CMT removes the need for transaction-related JNDI lookups - although of course the use of EJB itself necessitates the use of JNDI. It removes most of the need (although not entirely) to write Java code to control transactions. The significant downside is that CMT is tied to JTA and an application server environment. Also, it is only available if one chooses to implement business logic in EJBs, or at least behind a transactional EJB facade. The negatives around EJB in general are so great that this is not an attractive proposition, especially in the face of compelling alternatives for declarative transaction management.
Local Transactions. Local transactions may be easier to use, but have significant disadvantages: they cannot work across multiple transactional resources. For example, code that manages transactions using a JDBC connection cannot run within a global JTA transaction. Another downside is that local transactions tend to be invasive to the programming model.
Spring resolves these problems. It enables application developers to use a consistent programming model in any environment. You write your code once, and it can benefit from different transaction management strategies in different environments. The Spring Framework provides both declarative and programmatic transaction management. Declarative transaction management is preferred by most users, and is recommended in most cases.
With programmatic transaction management, developers work with the Spring Framework transaction abstraction, which can run over any underlying transaction infrastructure. With the preferred declarative model, developers typically write little or no code related to transaction management, and hence don't depend on the Spring Framework's transaction API (or indeed on any other transaction API).

We can use Hibernate/Eclipselink/Toplink,etc as the unerlying ORM technology, since I am familier with JPA 2 and its also the default implementation of glassfish I would always prefer to go with eclipselink for my web applications. Configured with Spring 3.1.2 release and mysql database and tested in glassfish 3.1.2.2 server.


Is an application server needed for transaction management?
The Spring Framework's transaction management support significantly changes traditional thinking as to when a J2EE application requires an application server.
In particular, you don't need an application server just to have declarative transactions via EJB. In fact, even if you have an application server with powerful JTA capabilities, you may well decide that the Spring Framework's declarative transactions offer more power and a much more productive programming model than EJB CMT.
Typically you need an application server's JTA capability only if you need to enlist multiple transactional resources, and for many applications being able to handle transactions across multiple resources isn't a requirement. For example, many high-end applications use a single, highly scalable database (such as Oracle 9i RAC). Standalone transaction managers such as Atomikos Transactions and JOTM are other options. (Of course you may need other application server capabilities such as JMS and JCA.)
The most important point is that with the Spring Framework you can choose when to scale your application up to a full-blown application server. Gone are the days when the only alternative to using EJB CMT or JTA was to write code using local transactions such as those on JDBC connections, and face a hefty rework if you ever needed that code to run within global, container-managed transactions. With the Spring Framework, only configuration needs to change so that your code doesn't have to.


Below you can see the JTA configuration with entityManagerFactory bean created from LocalContainerEntityManagerFactoryBean class


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
                http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" default-autowire="byName">


<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="xxxx"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter" >
                <property name="showSql" value="false" />
                <property name="generateDdl" value="false" />
                <property name="databasePlatform" value="org.eclipse.persistence.platform.database.MySQLPlatform" />
            </bean>
        </property>
        <property name="loadTimeWeaver">
            <bean class="org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver"/>
        </property>
    </bean>               
        
        <tx:annotation-driven /> 
        <tx:jta-transaction-manager/> 
        
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />


</beans>


JTA configuration with entityManagerFactory bean created from JNDI


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
                http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" default-autowire="byName">    
    
    <jee:jndi-lookup id="entityManagerFactory" jndi-name="jdbc/xxxx"/>
    <tx:annotation-driven /> 
    <tx:jta-transaction-manager/> 
        
    <bean
        class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
</beans>

If we are using jndi lookup in spring configuration file. Its necessary to place persistence-unit-ref in web.xml

web.xml
<persistence-unit-ref>
        <persistence-unit-ref-name>jdbc/xxxx</persistence-unit-ref-name>
        <persistence-unit-name>xxxx</persistence-unit-name>
    </persistence-unit-ref>

persistence.xml ( JTA )

<?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="xxxx" transaction-type="JTA">
    <jta-data-source>jdbc/xxxx</jta-data-source>
    <properties>
      <property name="eclipselink.jpa.uppercase-column-names" value="true"/>      
      <property name="eclipselink.cache.shared.default" value="false"/> 
      <property name="javax.persistence.lock.timeout" value="1000"/>     
      <property name="eclipselink.logging.level" value="SEVERE" />      
      <property name="eclipselink.target-server" value="SunAS9"/>
    </properties>
  </persistence-unit>
</persistence>


Below you can see the RESOURCE_LOCAL configuration with entityManagerFactory bean created from LocalContainerEntityManagerFactoryBean class

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" default-autowire="byName">

    
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">        
        <property name="persistenceUnitName" value="xxxx"/>
        <property name="persistenceXmlLocation" value="classpath:META-INF/test-persistence.xml"/>
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
        <property name="jpaDialect">
            <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect"/>
        </property>
        <property name="jpaPropertyMap">
            <props>
                <prop key="eclipselink.weaving">false</prop>
            </props>
        </property>

        <property name="loadTimeWeaver">
            <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver">
            </bean>
        </property>        
    </bean>

    <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
        <property name="databasePlatform" value="org.eclipse.persistence.platform.database.MySQLPlatform" />
        <property name="generateDdl" value="false"/>
        <property name="showSql" value="true"/>
    </bean>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${db.driver}" />
        <property name="url" value="${db.url}" />
        <property name="username" value="${db.username}" />
        <property name="password" value="${db.password}" />
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <tx:annotation-driven />

    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

</beans>


persistence.xml ( RESOURCE_LOCAL )


<?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="xxxx" transaction-type="RESOURCE_LOCAL">
    <class>xxxx</class>    
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties/>
  </persistence-unit>
</persistence>

To create web applications I always use maven archetype that helps to automate the build process, dependency management, SCM, etc. If you find any difficulty in getting the required dependencies please use the below pom.xml file.
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <jetty.version>6.1.4</jetty.version>
        <spring.version>3.1.2.RELEASE</spring.version>
        <spring.security.version>3.1.0.RELEASE</spring.security.version>
        <slf4j.version>1.5.10</slf4j.version>
        <java.version>1.6</java.version>
        <junit.version>4.10</junit.version>
        <mysql.connector.version>5.1.16</mysql.connector.version>
        <netbeans.hint.deploy.server>gfv3ee6</netbeans.hint.deploy.server>
    </properties>

    <dependencies>
     
         <dependency>
            <groupId>org.glassfish.extras</groupId>
            <artifactId>glassfish-embedded-all</artifactId>
            <version>3.2-b06</version>
            <scope>provided</scope>
        </dependency>  
               
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-core-asl</artifactId>
            <version>0.9.8</version>
        </dependency>  
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>3.5</version>
        </dependency>
        <dependency>
            <groupId>org.primefaces.themes</groupId>
            <artifactId>all-themes</artifactId>
            <version>1.0.9</version>
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>2.2.2</version>
        </dependency>


        <!-- Spring Dependencies -->

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
            <exclusions>
                <!-- Exclude Commons Logging in favor of SLF4j -->
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
            <type>jar</type>
            <scope>test</scope>
        </dependency>

        <!-- spring security -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>${spring.security.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>${spring.security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>${spring.security.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- DB Dependencies -->
        <!-- MySQL Connector -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.connector.version}</version>
            <type>jar</type>
            <scope>provided</scope>
        </dependency>
        <!-- Test Dependencies -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jettison</groupId>
            <artifactId>jettison</artifactId>
            <version>1.3.3</version>
        </dependency>
       
        <!-- logging -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>${slf4j.version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
            <exclusions>
                <exclusion>
                    <groupId>com.sun.jdmk</groupId>
                    <artifactId>jmxtools</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jmx</groupId>
                    <artifactId>jmxri</artifactId>
                </exclusion>
            </exclusions>
            <scope>compile</scope>
        </dependency>
<dependency>
            <groupId>log4j</groupId>
            <artifactId>apache-log4j-extras</artifactId>
            <version>1.1</version>
        </dependency>

    </dependencies>


I would recommend JTA configuration for production environment and RESOURCE_LOCAL for unit testing. With RESOURCE_LOCAL we have to take care of connection pooling and any change in connection pooling needs an application redepolyment. But with JTA, your application server provides connection pooling, transaction management and at run time we can change the connection pool settings without the need for application redeployment.