JPA批量更新似乎每批插入一个更新

时间:2014-05-02 16:08:33

标签: jpa eclipselink jpa-2.0

我正在尝试使用JPA / EclipseLink 2.4.1和使用Merlia驱动程序的MSSQL Server进行批量持久化。根据数据库与日志(下图)的反应方式,似乎每个插入都在其自己的批处理中发生。

我已将持久性xml设置如下:

<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
  <persistence-unit name="EclipseLinkPersistenceUnit">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>com.mypackage.ViewInDatabaseEntity</class>
    ...
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
      <property name="javax.persistence.jdbc.driver" value="com.inet.tds.PDataSource4"/>
      <property name="eclipselink.jdbc.batch-writing" value="jdbc" />
      <property name="eclipselink.jdbc.batch-writing.size" value="1000" />
      <property name="eclipselink.logging.level" value="FINEST"/>
    </properties>
  </persistence-unit>
</persistence>

调用persist的代码如下所示。我通常使用@Transactional,但决定手动开始并提交事务,以确保注释没有做出意想不到的事情。

  //@Transactional
  public void persistEntity(List<ViewInDatabaseEntity> entitiesToPersist) {
    EntityManager entityManager = entityManagerProvider.get();
    entityManager.setFlushMode(FlushModeType.COMMIT);
    EntityTransaction transaction = entityManager.getTransaction();
    transaction.begin();

    for (ViewInDatabaseEntity entity : entitiesToPersist) {
      entityManager.persist(entity);
    }

    transaction.commit();
  }

日志(设置为FINEST)正在输出:

[EL Finer]: transaction: 2014-05-02 16:42:49.963--ClientSession(27801544)--Connection(28732068)--Thread(Thread[qtp18824904-22,5,main])--begin transaction
...
[EL Finest]: query: 2014-05-02 16:42:49.964--ClientSession(27801544)--Thread(Thread[qtp18824904-22,5,main])--Execute query ValueReadQuery(name="SEQ_GEN_SEQUENCE" sql="SELECT @@IDENTITY")
[EL Finer]: sql: 2014-05-02 16:42:56.422--ClientSession(27801544)--Connection(28732068)--Thread(Thread[qtp18824904-22,5,main])--Begin batch statements
[EL Fine]: sql: 2014-05-02 16:42:56.422--ClientSession(27801544)--Connection(28732068)--Thread(Thread[qtp18824904-22,5,main])--INSERT INTO dbo.ViewInDatabaseEntity(property1, property4, property3, property2, property5) VALUES (?, ?, ?, ?, ?)
[EL Fine]: sql: 2014-05-02 16:42:56.422--ClientSession(27801544)--Connection(28732068)--Thread(Thread[qtp18824904-22,5,main])-- bind => [ABC, DEF, GHI, JKL, 0.3465443]
[EL Finer]: sql: 2014-05-02 16:42:56.422--ClientSession(27801544)--Connection(28732068)--Thread(Thread[qtp18824904-22,5,main])--End Batch Statements
[EL Fine]: sql: 2014-05-02 16:42:56.505--ClientSession(27801544)--Connection(28732068)--Thread(Thread[qtp18824904-22,5,main])--SELECT @@IDENTITY
[EL Finest]: sequencing: 2014-05-02 16:42:56.506--UnitOfWork(15091453)--Thread(Thread[qtp18824904-22,5,main])--assign sequence to the object (89,588 -> com.mypackage.ViewInDatabaseEntity@945ed6f5)
[EL Finest]: query: 2014-05-02 16:42:56.507--UnitOfWork(15091453)--Thread(Thread[qtp18824904-22,5,main])--Execute query InsertObjectQuery(com.mypackage.ViewInDatabaseEntity@5d6e305d)
[EL Finest]: query: 2014-05-02 16:42:56.507--ClientSession(27801544)--Thread(Thread[qtp18824904-22,5,main])--Execute query ValueReadQuery(name="SEQ_GEN_SEQUENCE" sql="SELECT @@IDENTITY")
[EL Finer]: sql: 2014-05-02 16:42:56.507--ClientSession(27801544)--Connection(28732068)--Thread(Thread[qtp18824904-22,5,main])--Begin batch statements
[EL Fine]: sql: 2014-05-02 16:42:56.507--ClientSession(27801544)--Connection(28732068)--Thread(Thread[qtp18824904-22,5,main])--INSERT INTO dbo.ViewInDatabaseEntity(property1, property4, property3, property2, property5) VALUES (?, ?, ?, ?, ?)
[EL Fine]: sql: 2014-05-02 16:42:56.507--ClientSession(27801544)--Connection(28732068)--Thread(Thread[qtp18824904-22,5,main])-- bind => [ABC, ZYX, WVU, RST, 0.35345634]
[EL Finer]: sql: 2014-05-02 16:42:56.507--ClientSession(27801544)--Connection(28732068)--Thread(Thread[qtp18824904-22,5,main])--End Batch Statements
[EL Fine]: sql: 2014-05-02 16:42:56.591--ClientSession(27801544)--Connection(28732068)--Thread(Thread[qtp18824904-22,5,main])--SELECT @@IDENTITY
[EL Finest]: sequencing: 2014-05-02 16:42:56.592--UnitOfWork(15091453)--Thread(Thread[qtp18824904-22,5,main])--assign sequence to the object (89,666 -> com.mypackage.ViewInDatabaseEntity@5d6e305d)
...
[EL Finer]: transaction: 2014-05-02 16:42:56.598--ClientSession(27801544)--Connection(28732068)--Thread(Thread[qtp18824904-22,5,main])--commit transaction
[EL Finest]: connection: 2014-05-02 16:42:56.605--ServerSession(2644459)--Connection(28732068)--Thread(Thread[qtp18824904-22,5,main])--Connection released to connection pool [default].

这似乎表明每个插入都发生在它自己的批处理中。根据persistence.xml;我启用了

<property name="eclipselink.jdbc.batch-writing" value="jdbc" />

据我所知,这些插入应该作为批处理发生。 以下是我试图坚持的实体。它作为一个View存在,覆盖了MSSQL Server DB中的几个基础表。

@javax.persistence.Table(name = "ViewInDatabase", schema = "dbo")
@Entity
@EqualsAndHashCode
@Getter
@Setter
public class ViewInDatabaseEntity {

  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE)
  private int id;

  @javax.persistence.Column(name = "id", nullable = false, insertable = false, updatable = false,
        length = 0, precision = 0)
  @Basic
  public int getId() {
    return id;
  }

  @javax.persistence.Column(name = "property1", nullable = false, insertable = true,
        updatable = true, length = 20, precision = 0)
  @Basic
  private String property1;

  @javax.persistence.Column(name = "property2", nullable = true, insertable = true, updatable = true,
        length = 0, precision = 0)
  @Basic
  private String property2;

  @javax.persistence.Column(name = "property3", nullable = true, insertable = true,
        updatable = true, length = 0, precision = 0)
  @Basic
  private String property3;

  @javax.persistence.Column(name = "property4", nullable = false, insertable = true, updatable = true,
        length = 10, precision = 0)
  @Basic
  private String property4;

  @javax.persistence.Column(name = "property5", nullable = true, insertable = true, updatable = true,
        length = 0, precision = 0)
  @Basic
  private BigDecimal property5;

  @javax.persistence.Column(name = "property6", nullable = false, insertable = false,
        updatable = false, length = 0, precision = 0)
  @Basic
  private int property6;

  @javax.persistence.Column(name = "property7", nullable = false, insertable = false,
        updatable = false, length = 0, precision = 0)
  @Basic
  private Date property7;
}

1 个答案:

答案 0 :(得分:1)

当序列策略要求在每个insert语句之后查找指定的序列值时,批处理将不起作用。尝试使用表序列或一些允许预先分配序列值的策略,以便可以对它们进行批处理。