Container managed Transaction for JPA+Spring+Hibernate in Tomcat

I am trying to configure JOTM with JPA,Spring, hibernate and tomcat. I have configured somehow but neither it perform inserts nor updates, in other words, i am unable to perform commits after inserting/updating data. I have following configuration.

%TOMCAT_HOME%/conf/context.xml

<?xml version="1.0" encoding="utf-8"?>
<Context>
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <Resource name="jdbc/myDS" auth="Container" type="javax.sql.XADataSource" factory="org.objectweb.jotm.datasource.DataSourceFactory" driverClassName="com.mysql.jdbc.Driver" username="root" password="mypassword" url="jdbc:mysql://localhost:3306/myDB"/>
    <Resource name="UserTransaction" auth="Container" type="javax.transaction.UserTransaction"/>
    <Transaction factory="org.objectweb.jotm.UserTransactionFactory" jotm.timeout="60"/>
</Context>

JOTM 2.1.9 libaries extracted in %TOMCAT%/lib

          Jotm-datasource.jar
·         Jotm-core.jar
·         Xapool.jar
·         Log4j.jar
·         Commons-loggin-api.jar
·         Carol-iiop-delegate.jar
·         Carol-interceptors.jar
·         Carol.jar
·         Ow2-jta-1.1-spec.jar
·         Ow2-connector-1.5-spec.jar
·         Howl.jar

src/main/webapp/META-INF/context.xml

<Context path="/MyJPATest" reloadable="true">
    <Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"/>
</Context>

src/main/webapp/WEB-INF/classes/carol.properties

# JNDI (Protocol Invocation)

carol.protocols=jrmp

# Local RMI Invocation
carol.jvm.rmi.local.call=true


# do not use CAROL JNDI wrapper
carol.start.jndi=false

# do not start a name server
carol.start.ns=false

# Naming Factory
carol.jndi.java.naming.factory.url.pkgs=org.apache.naming

META-INF/Persistence.xml

<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
    <persistence-unit name="myPersistenceUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>java:comp/env/jdbc/myDS</jta-data-source>
        <class>com.test.domain.PUser</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
        </properties>
    </persistence-unit>
</persistence>

applicationContext.xml

 <bean
        id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >
        <property
            name="persistenceUnitName"
            value="myPersistenceUnit" />
    </bean>


    <context:annotation-config />

    <jee:jndi-lookup
        id="jndiUserTransaction"
        jndi-name="java:comp/UserTransaction" />

    <bean
        id="transactionManager"
        class="org.springframework.transaction.jta.JtaTransactionManager" >

        <property
            name="userTransaction"
            ref="jndiUserTransaction" />
    </bean>


    <tx:annotation-driven transaction-manager="transactionManager" />

      <jpa:repositories
        base-package="com.test"
        entity-manager-factory-ref="entityManagerFactory" />



    <bean
        id="hibernate4ExceptionTranslator"
        class="org.springframework.orm.hibernate4.HibernateExceptionTranslator" />

web.xml

<resource-ref>
        <res-ref-name>jdbc/myDS</res-ref-name>
        <res-type>javax.sql.XADataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>

PUserService's method

 @Transactional
    public List<PUser> bootstrapUsers( )
    {

        List<PUser> users = new ArrayList<PUser>();

        try
        {


            for (int i = 0; i < 5; i++)
            {

                PUser newUser = new PUser();
                newUser.setName("B_" + i);
                PUser user = this.pUserRepository.save(newUser);


                users.add(user);

            }

        }
        catch (Exception e)
        {
            this.logger.error(e, e);

            return null;
        }

        return users;

    }

src/main/java/com.test.dao.PUserRepository (Spring-data repository)

public interface PUserRepository extends JpaRepository<PUser, Serializable>
{

    PUser findByName(String username);
}

Thanks in advance.

Answers


Since you are using JTA your transaction-type should be transaction-type="JTA" in your persistence.xml. You can read more on this here under Configuration and bootstrapping.


Need Your Help

MySQL order posts by most recent comment OR last posted

mysql sql select sql-order-by

How can I sort posts so most recent activity is on top?

Facebook Ads API image use my CDN url

facebook facebook-ads-api

I'm working with the Facebook Ads API, and i'd like to know if is there possible to they deliver the image from my CDN not theirs; or is it only possible in the FBx RTB?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.