Spring / Hibernate не фиксирует все записи в базе данных - PullRequest
0 голосов
/ 22 апреля 2020

Я видел несколько SO сообщений об этой теме c, но до сих пор не нашел решения. У меня есть приложение Spring Batch, которое вставляет сотни записей в базу данных Postgresql. Но я обнаружил, что от 1 до 15 записей никогда не фиксируются. Я включил ВСЕ ведение журнала, вплоть до уровня TRACE, и вижу вызов transaction.commit() для одной из записей, которые не были обновлены. Я не вижу ничего неупорядоченного, что могло бы привести к тому, что определенные записи не будут зафиксированы.

Вот мое application.properties:

spring.jpa.hibernate.use-new-id-generator-mappings=true
spring.jpa.properties.hibernate.id.new_generator_mappings=true
spring.jpa.properties.hibernate.jdbc.time_zone = UTC
spring.jpa.properties.hibernate.jdbc.batch_size=500
spring.jpa.properties.hibernate.order_inserts=true
spring.jpa.properties.hibernate.order_updates=true
spring.jpa.properties.hibernate.type=trace
spring.jpa.properties.org.hibernate.flushMode=ALWAYS
spring.jpa.properties.hibernate.generate_statistics=true 

Вот мой метод обслуживания:

//@Transactional(propagation = Propagation.REQUIRES_NEW) -- removed to see if the behavior was the same
@Override
public RefreshJobDetailEntity updateRefreshJobDetail(String refreshJobId, String domainCode, String code, Date effectiveDate,
                                                     RefreshJobDetailStatusEnum status, String userId) {

    [snip]

    Date completionTime = Calendar.getInstance().getTime();
    entity.setRecordCompletionTime(completionTime);
    entity.setLastUpdateTimeStamp(completionTime);
    entity.setLastUpdateUserNumber(entity.getOrignalUserNumber());
    entity.setRecordStatus(status.getValue());

    [snip]

    refreshJobDetailRepository.saveAndFlush(entity);
    entityManager.clear();

    return entity;
}

А вот журнал, отражающий фиксацию:

10:22:19.541 [ERDMThreadPoolExecutor-24] DEBUG c.t.e.b.common.ERDTransactionManager - Initiating transaction commit
10:22:19.541 [ERDMThreadPoolExecutor-24] DEBUG c.t.e.b.common.ERDTransactionManager - Committing JPA transaction on EntityManager [org.hibernate.jpa.internal.EntityManagerImpl@146f4f7d]
10:22:19.541 [ERDMThreadPoolExecutor-24] DEBUG o.h.e.t.internal.TransactionImpl - committing
10:22:19.541 [ERDMThreadPoolExecutor-24] TRACE o.h.r.t.b.j.i.JdbcResourceLocalTransactionCoordinatorImpl - ResourceLocalTransactionCoordinatorImpl#beforeCompletionCallback
10:22:19.542 [ERDMThreadPoolExecutor-24] TRACE org.hibernate.internal.SessionImpl - SessionImpl#beforeTransactionCompletion()
10:22:19.542 [ERDMThreadPoolExecutor-24] TRACE org.hibernate.internal.SessionImpl - Automatically flushing session
10:22:19.542 [ERDMThreadPoolExecutor-24] TRACE o.h.r.t.i.SynchronizationRegistryStandardImpl - SynchronizationRegistryStandardImpl.notifySynchronizationsBeforeTransactionCompletion
10:22:19.542 [ERDMThreadPoolExecutor-24] TRACE o.h.r.j.i.AbstractLogicalConnectionImplementor - Preparing to commit transaction via JDBC Connection.commit()
10:22:19.552 [ERDMThreadPoolExecutor-24] TRACE o.h.r.j.i.AbstractLogicalConnectionImplementor - Transaction committed via JDBC Connection.commit()

Так что может быть причиной того, что записи могут отражать обновление, которое делает мой метод обслуживания? Из 675 записей, большинство будет обновляться нормально. Но всегда есть несколько таких, которые не запускаются, и я полностью озадачен и деморализован ... Очень признателен за любой ввод.

РЕДАКТИРОВАТЬ: я выпускаю save / flu sh вместе с очисткой entityManager, чтобы я мог видеть обновления базы данных сразу, хотя Spring Batch все еще работает. Это жесткое и быстрое требование для моего приложения, и я не мог найти другого пути.

...