如何使用Entity Manager将每个事务刷新到DB

时间:2012-12-06 15:23:16

标签: java hibernate jpa seam jta

我是使用smooks从.csv文件中获取记录,文件大小在单个文件中大约是30MB数据,在这个文件中大约有3条记录被提取到List ..

我完成了下一个List分为子部分,使用子列表分区大小最多为2000。

我想在单个事务中刷新2000条记录。但是我的代码中不允许这样做。

我正在使用seam 2.1.2,jap with hibernate,EntityManager,JTA Transactions。

components.xml

  <core:init debug="false" jndi-pattern="@jndiPattern@" />
    <core:manager concurrent-request-timeout="2000"
        conversation-id-parameter="cid" conversation-timeout="120000"
        parent-conversation-id-parameter="pid" />
    <web:hot-deploy-filter url-pattern="/*.mobee" />
    <persistence:entity-manager-factory
        installed="@seamBootstrapsPu@" name="entityManagerFactory"
        persistence-unit-name="mobeeadmin" />
    <persistence:managed-persistence-context 
        auto-create="true" entity-manager-factory="@seamEmfRef@" name="entityManager"
        persistence-unit-jndi-name="@puJndiName@" />
 <async:quartz-dispatcher />
    <security:identity authenticate-method="#{authenticator.authenticate}" />
  <web:rewrite-filter view-mapping="*.mobee" />
    <web:multipart-filter create-temp-files="true" max-request-size="28672000"     url-pattern="*.seam"/>
    <event type="org.jboss.seam.security.notLoggedIn">
        <action execute="#{redirect.captureCurrentView}" />
    </event>
<event type="org.jboss.seam.security.loginSuccessful">
        <action execute="#{redirect.returnToCapturedView}" />
    </event>
    <mail:mail-session host="localhost" port="25" />

Java代码:

  private  List<DoTempCustomers> doTempCustomers;

   int partitionSize = 2000;

   for (int i = 0; i < doTempCustomers.size(); i += partitionSize) {
      String message= tempCustomerMigration(doTempCustomers.subList(i,
               i + Math.min(partitionSize, doTempCustomers.size() - i)));
   }



 @Begin(join=true)
    public String tempCustomerMigration(List<DoTempCustomers>  list){
         PersistenceProvider.instance().setFlushModeManual(getEntityManager());
         TempCustomers temp = null;
      for(DoTempCustomers tempCustomers:list){
        try {
        temp=new TempCustomers();
            BeanUtils.copyProperties(temp, tempCustomers);
            getEntityManager.persist();
            getEntityManager.flush();
         }

我尝试了很多次这个问题从来没有解决过如何在发送服务器响应GUI之前在每个事务中刷新记录到数据库

否则我得到例外的一些处理时间是

2012-12-06 17:09:56,380 WARN  [com.arjuna.ats.arjuna.logging.arjLoggerI18N] [com.arjuna.ats.arjuna.coordinator.BasicAction_58] - Abort of action id -53eff40e:f2db:50c0a356:7d invoked while multiple threads active within it.
2012-12-06 17:09:56,380 WARN  [com.arjuna.ats.arjuna.logging.arjLoggerI18N] [com.arjuna.ats.arjuna.coordinator.CheckedAction_2] - CheckedAction::check - atomic action -53eff40e:f2db:50c0a356:7d aborting with 1 threads active!
2012-12-06 17:09:56,522 DEBUG [org.jboss.util.NestedThrowable] org.jboss.util.NestedThrowable.parentTraceEnabled=true
2012-12-06 17:09:56,522 DEBUG [org.jboss.util.NestedThrowable] org.jboss.util.NestedThrowable.nestedTraceEnabled=false
2012-12-06 17:09:56,522 DEBUG [org.jboss.util.NestedThrowable] org.jboss.util.NestedThrowable.detectDuplicateNesting=true
2012-12-06 17:09:56,524 INFO  [STDOUT] [Mobee]- WARN 2012-12-06 17:09:56,524 [] JDBCExceptionReporter - SQL Error: 0, SQLState: null
2012-12-06 17:09:56,525 INFO  [STDOUT] [Mobee]-ERROR 2012-12-06 17:09:56,524 [] JDBCExceptionReporter - Transaction is not active: tx=TransactionImple < ac, BasicAction: -53eff40e:f2db:50c0a356:7d status: ActionStatus.ABORTING >; - nested throwable: (javax.resource.ResourceException: Transaction is not active: tx=TransactionImple < ac, BasicAction: -53eff40e:f2db:50c0a356:7d status: ActionStatus.ABORTING >)
2012-12-06 17:09:56,545 INFO  [STDOUT] [Mobee]-ERROR 2012-12-06 17:09:56,527 [] AbstractFlushingEventListener - Could not synchronize database state with session
org.hibernate.exception.GenericJDBCException: Cannot open connection
    at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)

对于这个例外,我在google中找到了sol来增加到jboss - service.xml中的T​​ransactionTimeout对我来说,甚至没有增加超时参数。

1 个答案:

答案 0 :(得分:0)

您可以创建一个新的Seam组件,它是一个EJB会话Bean,并使用UserTransaction批量执行更新/插入。 UserTransaction还允许您指定事务超时。您可以将此新组件注入上面使用的组件中。 Here is an example - see the 5th post.。否则,如果您不想使用EJB,我认为您的方法需要您使用嵌套的Seam对话,因为看起来您使用的是Seam managed persistence context,其范围限定为单个会话。