Pergunta

We are trying to setup a new JTA transaction configuration for our new project. This project make use of spring JPA and spring Data with atomikos JTA transaction manager.

We configured our xmls as we saw in one of the atomikos tutorials.

Currently we managed to startup our server with no error.

Problem is when we try to persist an entity with the jpaRepository within a @Transactional service, our logs shows that the atomikos jta tranascion is taking place and commit succues. But our DB shows now new entry in the table.

We had similar issue before when 2 beans scanned the same packages and resulting with "psuedo persist" effect like above.

here is our xml:

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

<tx:annotation-driven transaction-manager="transactionManager"
    proxy-target-class="true" />

<bean id="AtomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
    init-method="init" destroy-method="close">
    <property name="forceShutdown" value="false" />
</bean>

<bean id="AtomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
    <property name="transactionTimeout" value="300" />
</bean>

<bean id="transactionManager"
    class="org.springframework.transaction.jta.JtaTransactionManager"
    depends-on="AtomikosTransactionManager,AtomikosUserTransaction">
    <property name="transactionManager" ref="AtomikosTransactionManager" />
    <property name="userTransaction" ref="AtomikosUserTransaction" />
    <property name="allowCustomIsolationLevels" value="true" />
</bean>

<bean id="dataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean"
    init-method="init" destroy-method="close">

    <property name="uniqueResourceName" value="DataSource" />
    <property name="xaDataSource" ref="dataBase" />
    <property name="poolSize" value="3" />

</bean>

<bean id="dataBase" class="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"
    lazy-init="true">

    <property name="pinGlobalTxToPhysicalConnection" value="true" />
    <property name="url"
        value="mydb" />
    <property name="user" value="user" />
    <property name="password" value="password" />

</bean>

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="packagesToScan"
        value="packages" />
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
        </props>
    </property>
    <property name="dataSource" ref="dataSource" />
    <property name="jpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
    </property>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="database" value="MYSQL" />
            <!-- <property name="databasePlatform" value="${hibernate.dialect}"/> -->
            <property name="showSql" value="false" />
            <property name="generateDdl" value="false" />
            <!-- <property name="hibernate.connection.autocommit" value="false"/> -->
        </bean>
    </property>
    <property name="jpaPropertyMap">
        <map>
            <entry key="hibernate.connection.autocommit" value="false" />
        </map>
    </property>
</bean>

Any ideas what's wrong with this configuration file?

Thanks!

Foi útil?

Solução

It turns out we were missing some properties in jpaPropertyMap in the entityManagerFactory. This is the correct configuration for it:

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="packagesToScan"
        value="packages" />
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
        </props>
    </property>
    <property name="dataSource" ref="dataSource" />
    <property name="jpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
    </property>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="database" value="MYSQL" />
            <property name="showSql" value="false" />
            <property name="generateDdl" value="false" />
        </bean>
    </property>
    <property name="jpaPropertyMap">
        <map>
            <entry key="javax.persistence.transactionType" value="JTA" />
            <entry key="hibernate.current_session_context_class" value="jta" />
            <entry key="hibernate.transaction.manager_lookup_class"
                value="com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup" />
            <entry key="hibernate.connection.autocommit" value="false" />
        </map>
    </property>
</bean>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top