Persist not working when ObjectDB and another db is used in the same application (through spring) with different tx managers

#1

Hi, 

Object are not being persisted when ObjectDB and another db is used in the same application (through spring) with different tx managers, please find an example of this below. Any help is greatly appreciate as we are currently evaluating ObjectDB to use in our new enterprise application and we need to decide if we are going to purchase it very soon.

Many Thanks

 

Persistence.xml

<?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="testPU" transaction-type="RESOURCE_LOCAL">
  <provider>com.objectdb.jpa.Provider</provider>
  <properties>
   <property name="javax.persistence.jdbc.url" value="$objectdb/db/test.odb"/>
   <property name="javax.persistence.jdbc.user" value="admin"/>
   <property name="javax.persistence.jdbc.password" value="admin"/>
  </properties>
</persistence-unit>

</persistence>

Spring xml

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
        http://www.springframework.org/schema/beans    
        http://www.springframework.org/schema/beans/spring-beans-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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<context:annotation-config />
<context:component-scan base-package="com.prohire" />

<mvc:annotation-driven />
         
<!-- Datasources -->
  <bean id="dataSource" destroy-method="close"
    class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  <property name="url" value="jdbc:mysql://localhost:3306/streaming_test" />
  <property name="username" value="root" />
  <property name="password" value="" />
  <property name="validationQuery" value="SELECT 1" />
</bean>



<bean id="entityManagerFactory"
  class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="packagesToScan" value="com.prohire" />
  <property name="jpaVendorAdapter">
   <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
  </property>
  <property name="persistenceUnitName" value="mysqlPU" />
  <property name="jpaProperties">
   <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    <prop key="hibernate.format_sql">true</prop>
    <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
   </props>
  </property>
</bean>

<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
  <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="txManager" />

<bean id="emf" class=
       "org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
       <property name="loadTimeWeaver">
        <bean class=
"org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
      </property>
      <property name="persistenceXmlLocation" value="classpath:persistence.xml" />
  </bean>

  <!-- Add Transaction support -->
  <bean id="myTxManager"
     class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="emf"/>
  </bean>

  <!-- Use @Transaction annotations for managing transactions -->
  <tx:annotation-driven transaction-manager="myTxManager" />

</beans>

 

Persistence code

@PersistenceContext(unitName = "testPU")
private EntityManager em;

@PersistenceContext(unitName = "mysqlPU")
private EntityManager em1;

@Transactional(readOnly = false)
public void addObject() {
 
  Transfer transfer = new Transfer();
  transfer.setId(UUID.randomUUID().toString());
  transfer.setNoOfBlocks(999999);
  transfer.setStartedAt(Calendar.getInstance().getTime());
  em.persist(transfer);

  List<Transfer> t = em.createQuery("SELECT t FROM Transfer t").getResultList();
 
  System.out.println(t.size());
}

@Transactional(readOnly = false)
public void addObjectw() {
 
  Transfer transfer = new Transfer();
  transfer.setId(UUID.randomUUID().toString());
  transfer.setNoOfBlocks(999999);
  transfer.setStartedAt(Calendar.getInstance().getTime());
  em1.persist(transfer);
 
  List<Transfer> t = em1.createQuery("SELECT t FROM Transfer t").getResultList();
 
  System.out.println(t.size());
 
}

 

#2

Both addObject and addObjectw are annotated with the same @Transactional annotation, which apparently takes the first declared EntityManagerFactory "entityManagerFactory", which is associated with the MySQL/Hibernate persistence unit.

You should be able to specify the ObjectDB transaction manager explicitly:

@Transactional(value ="myTxManager", readOnly = false)
public void addObject() {
  ...
}

In addition, you should also amend the ObjectDB persistence unit definition in Spring configuration, and specify the name of the persistence unit (you use a definition from our Spring Framework tutorial that is good only when there is only one persistence unit):

<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  <property name="persistenceUnitName" value="testPU" />
  <property name="loadTimeWeaver">
    <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
  </property>
  <property name="persistenceXmlLocation" value="classpath:persistence.xml" />
</bean
ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
#3

Thanks support, this fixed it, really appreciate the help. 


Post Reply

To post a reply and/or subscribe to update notifications - please login