Announcement Announcement Module
Collapse
No announcement yet.
@Transactional not being honored using Extended PC Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @Transactional not being honored using Extended PC

    I have set up JPA with a JTA TX Mgr (Geronimo, fusesource.com's servicemix TX mgr). The TX mgr is exposed via a osgi:reference. Im using Extended PC's. These are being injected (properly) into my Repository code, but when i throw a exception, i dont see the rollback happen down to the database. My camel route thats processing this does in fact rollback. Its marked as transacted(). Ive been wading thru spring tx and spring aop to see whats happening but so far im at a loss. Below is my configuration & code.

    It is interesting to note that it does in fact save the data to the database, and the rollback does not take place. I assume there is some transaction going on there behind the scenes. It is also interesting to note that NO TX advice works, neither in aop advice or xmlns:tx. I went so far to mark TX's as readonly and data still saves. I have also included the debug logs from servicemix regarding the spring and hibernate code. I also made sure the Repository was a prototype as per the javadoc.

    #############

    JTAPersonRepository
    Code:
    @Transactional
    public class JTAPersonRepository {
    
        @PersistenceContext(unitName = "person")
        private EntityManager entityManager;
    
    	public Person saveThenRollback() throws Exception {
    	    Person p = new Person();
            p.setName("This should fail");
            p.setSsn("12345");
            entityManager.persist(p);
            throw new NullPointerException("Purposeful FAIL :::: to demonstrate JTA");
        }
    }
    spring-persistence.xml
    Code:
    <?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:cxfse="http://servicemix.apache.org/cxfse/1.0"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:osgi="http://www.springframework.org/schema/osgi" xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="
                    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                    http://servicemix.apache.org/cxfse/1.0 http://servicemix.apache.org/cxfse/1.0/servicemix-cxf-se.xsd
                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
                    http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd
                    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    ">
    
        <bean id="personRepository" class="com.test.JTAPersonRepository" scope="prototype"/>
    
        <bean id="jpaTemplate" class="org.springframework.orm.jpa.JpaTemplate">
            <property name="entityManagerFactory" ref="entityManagerFactory"/>
        </bean>
    
        <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="persistenceUnitName" value="person"/>
            <property name="jpaVendorAdapter" ref="jpaAdapter"/>
        </bean>
    
        <bean id="jpaAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect"/>
        </bean>
    
        <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="false"/>
    
        <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
    
        <bean id="jpaPerson" class="org.apache.camel.component.jpa.JpaComponent">
            <property name="entityManagerFactory" ref="entityManagerFactory"/>
            <property name="transactionManager" ref="transactionManager"/>
        </bean>
    
        <osgi:reference id="transactionManager" interface="org.springframework.transaction.PlatformTransactionManager"/>
    
    </beans>
    persistence.xml
    Code:
    <?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="1.0">
    
        <persistence-unit name="person" transaction-type="JTA">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <jta-data-source>java:comp/env/jdbc/personDB</jta-data-source>
            <class>com.test.data.Person</class>
    
            <!-- Hibernate -->
            <properties>
                <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
                <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
                <property name="hibernate.connection.release_mode" value="after_transaction" />
                <property name="hibernate.transaction.flush_before_completion" value="false" />
                <property name="hibernate.connection.autocommit" value="false" />
                <property name="hibernate.show_sql" value="true" />
            </properties>
    
        </persistence-unit>
    </persistence>
    jndi.xml
    Code:
    <?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:beans="http://www.springframework.org/schema/beans"
           xsi:schemaLocation="http://www.springframework.org/schema/osgi
                                     http://www.springframework.org/schema/osgi/spring-osgi-1.0.xsd
                                     http://www.springframework.org/schema/beans
                                     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    
        <bean id="jndi"
              class="org.apache.xbean.spring.jndi.SpringInitialContextFactory"
              factory-method="makeInitialContext">
            <property name="entries">
                <map>
    
                    <entry key="java:comp/env/jdbc/personDB">
                        <!--DataSource Definition -->
                        <bean id="dataSourcePerson" class="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"
                              destroy-method="close">
                            <property name="url" value="jdbc:mysql://localhost:3306/person"/>
                            <property name="user" value="root"/>
                            <property name="password" value=""/>
                        </bean>
                    </entry>
                    
                    <entry key="java:comp/UserTransaction" value-ref="userTransaction"/>
                    <!-- Setting these for the tx mgr lookup -->
                    <entry key="UserTransaction" value-ref="userTransaction"/>
                    <entry key="java:/TransactionManager" value-ref="transactionManager"/>
                </map>
            </property>
        </bean>
        <bean id="userTransaction" class="org.springframework.jndi.JndiObjectFactoryBean">
            <property name="jndiName" value="osgi:services/javax.transaction.UserTransaction"/>
        </bean>
        <bean id="transactionManager" class="org.springframework.jndi.JndiObjectFactoryBean">
            <property name="jndiName" value="osgi:services/javax.transaction.TransactionManager"/>
        </bean>
    
    </beans>
Working...
X