无法使用JTA CMT持久保存/合并

时间:2012-02-03 03:59:00

标签: transactions ejb-3.0 glassfish-3 jta netbeans-7

我需要创建一个可以将数据从oracle DB复制到MSSQL DB的计划Web应用程序。 一旦部署了Web应用程序,所有编译的代码和表在MSSQL DB中成功创建。

但是来自oracle的数据提取只是不会在MSSQL DB中持久存在/合并,也不会例外或无论如何。

有什么建议为什么它不起作用?

具有2个PU的persistent.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="sourcePU" transaction-type="JTA">
    <jta-data-source>sourceConn</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
    <properties>
      <property name="eclipselink.canonicalmodel.subpackage" value="sourceName"/>
    </properties>
  </persistence-unit>
  <persistence-unit name="destPU" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>destConn</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
    <properties>
      <property name="eclipselink.canonicalmodel.subpackage" value="destName"/>
      <property name="eclipselink.ddl-generation" value="create-tables"/>
    </properties>
  </persistence-unit>
</persistence>

实体是自动生成的,并且基于它们创建了表格,所以我将把它们留下来。

负责复制的bean。

@Stateless
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public class FacadeReplicator {

    @PersistenceContext(unitName = "sourcePU") 
    private EntityManager emSource;    
    @PersistenceContext(unitName = "destPU") 
    private EntityManager emDest;

    protected EntityManager getEntityManagerSource() {
        return emSource;
    }

    protected EntityManager getEntityManagerDest() {
        return emDest;
    }

    public JourneyFacadeReplicator() {
        //super(Journey.class);
    }

    private List<MyEntity> getData(Timestamp fromDate, Timestamp toDate) {
        return getEntityManagerSource().createNamedQuery("MyEntity.findBetweenDates", MyEntity.class).setParameter("fromDate", fromDate, TemporalType.TIMESTAMP).setParameter("toDate", toDate, TemporalType.TIMESTAMP).getResultList();   
    }

    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    public int findMyEntityBetweenDates(Timestamp fromDate, Timestamp toDate) {
        List<MyEntity> test = getData(fromDate, toDate);

        Iterator<MyEntity> iter = test.iterator();
        while (iter.hasNext()) {
            MyEntity toUpdate = iter.next();
            getEntityManagerDest().merge(toUpdate);
            //getEntityManagerDest().flush();
        }
        return 1;
    }        
}

以下是来电者/时间表

@Stateless
public class Scheduler {   
    @EJB
    FacadeReplicator replicator;

    @Schedule(second="*", minute="*/2",hour="*", persistent=false)
    public void doWork(){
        Timestamp fromDate = Timestamp.valueOf("2011-06-24 00:00:00");
        Timestamp toDate = Timestamp.valueOf("2011-06-24 02:00:00");
        replicator.findMyEntityBetweenDates(fromDate, toDate);
    }        
}

PS:我使用的是netbean 7.0.1,EJB 3(或3.1?),glassfish 3.1.1,eclipselink,JTA CMT,Oracle DB 10g(ojdbc14),MSSQL 2008(最新的jdbc)。

感谢。

0 个答案:

没有答案