我有一个事务方法相互调用,但似乎事务提交应该提交的地点或至少在此之后出现插入日志。这是呼叫层次结构:
@Override
@Transactional
public DataProcessingStatistics copyInitialRevision() {
try {
DataProcessingStatistics statistics = new DataProcessingStatistics();
transaction(statistics);
statistics.printResult();
return statistics;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void transaction(DataProcessingStatistics statistics) {
entityAuditTableService.generateTables(statistics);
oneToManyRelationAuditTableService.generateTables(statistics);
manyToManyRelationAuditTableService.generateTables(statistics);
}
...
@Service
public class EntityAuditTableServiceImpl implements EntityAuditTableService {
...
@Override
public void generateTables(DataProcessingStatistics statistics) {
List<EntityAuditTable> auditTables = loadAll();
for (EntityAuditTable auditTable : auditTables) {
try {
statistics.addAsProcessed(auditTable, process(auditTable));
} catch (Exception e) {
statistics.addAsSkipped(auditTable.getAuditTable(), e);
}
}
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
public int process(EntityAuditTable auditTable) {
EntityDataTable table = entityDataTableService.create(auditTable.getEntityDataTable().getEntityClass());
List<EntityDataTableRow> rows = entityDataTableService.readAllData(table);
List<EntityAuditTableRow> auditTableRows = entityAuditTableRowService.generateTableData(auditTable, rows);
entityAuditTableRowService.saveAll(auditTableRows);
return auditTableRows.size();
}
...
@Service
public class EntityAuditTableRowServiceImpl implements EntityAuditTableRowService {
...
@Override
@Transactional
public void saveAll(List<EntityAuditTableRow> auditTableRows) {
jdbcService.saveArraysAs500RowChunks(auditTableRows, new ListSaver<EntityAuditTableRow>(){
@Override
public void save(List<EntityAuditTableRow> list) {
EntityAuditTableRowServiceImpl.this.saveJdbc(list);
for (EntityAuditTableRow auditTableRow: list) {
entityChangeService.saveFromRow(auditTableRow);
}
}
});
}
...
@Service
public class JdbcServiceImpl implements JdbcService {
@Override
public <E> void saveArraysAs500RowChunks(List<E> rows, ListSaver<E> saver) {
for (int i = 0; i < rows.size(); i += 500) {
save500(rows.subList(i, Math.min(i +500, rows.size())), saver);
}
}
@Override
@Transactional(propagation=Propagation.REQUIRES_NEW)
public <E> void save500(final List<E> rows, ListSaver<E> saver) {
if (rows == null || rows.size() == 0) {
return;
}
if (rows.size() > 500) {
throw new IllegalStateException("(auditTableRows.size() > 500)");
}
saver.save(rows);
}
...
@Service
public class EntityChangeServiceImpl implements EntityChangeService {
@Override
@Transactional
public void saveFromRow(EntityAuditTableRow auditTableRow) {
Revision revision = revisionsService.load(auditTableRow.getAuditTableRow().getRevision().getId());
EntityChange entityChange = create(auditTableRow);
save(entityChange);
revision.getEntitiesChanges().add(entityChange);
revisionsService.save(revision);
}
...
@Service
public class RevisionServiceImpl implements RevisionService {
@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public Revision create() {
Revision revisionEntity = createDetached();
long generateNextIdForClass = idService.generateNextIdForClass(Revision.class);
revisionEntity.setId(generateNextIdForClass);
revisionEntity.setTimestamp(new Date());
save(revisionEntity);
return revisionEntity;
}
@Override
@Transactional
public void save(Revision revisionEntity) {
persistenceManagerHibernate.save(revisionEntity);
}
事情是只有在从root / initial方法调用返回后才会收到ENTITY_CHANGED_IN_REVISION表/ EntityChange实体的日志中的插入:
这一点:
@Override
@Transactional
public DataProcessingStatistics copyInitialRevision() {
try {
DataProcessingStatistics statistics = new DataProcessingStatistics();
transaction(statistics);
statistics.printResult();
return statistics; // only after that return insert log appear
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
此日志仅在返回后显示:
Hibernate: insert into ENTITY_CHANGED_IN_REVISION (ENTITY_ID, ENTITY_NAME, GROUP_ENTITY_ID, GROUP_NAME, REV_ID, REVISION_TYPE, ID) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into ENTITY_CHANGED_IN_REVISION (ENTITY_ID, ENTITY_NAME, GROUP_ENTITY_ID, GROUP_NAME, REV_ID, REVISION_TYPE, ID) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into ENTITY_CHANGED_IN_REVISION (ENTITY_ID, ENTITY_NAME, GROUP_ENTITY_ID, GROUP_NAME, REV_ID, REVISION_TYPE, ID) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into ENTITY_CHANGED_IN_REVISION (ENTITY_ID, ENTITY_NAME, GROUP_ENTITY_ID, GROUP_NAME, REV_ID, REVISION_TYPE, ID) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into ENTITY_CHANGED_IN_REVISION (ENTITY_ID, ENTITY_NAME, GROUP_ENTITY_ID, GROUP_NAME, REV_ID, REVISION_TYPE, ID) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into ENTITY_CHANGED_IN_REVISION (ENTITY_ID, ENTITY_NAME, GROUP_ENTITY_ID, GROUP_NAME, REV_ID, REVISION_TYPE, ID) values (?, ?, ?, ?, ?, ?, ?)
从“保存500行块”方法返回后,而不是从@Transactional(propagation = Propagation.REQUIRES_NEW)返回后获取它们:
@Override
@Transactional(propagation=Propagation.REQUIRES_NEW)
public <E> void save500(final List<E> rows, ListSaver<E> saver) {
if (rows == null || rows.size() == 0) {
return;
}
if (rows.size() > 500) {
throw new IllegalStateException("(auditTableRows.size() > 500)");
}
saver.save(rows); // invokes EntityChangeServiceImpl.saveFromRow
}
...
@Service
public class EntityChangeServiceImpl implements EntityChangeService {
@Override
@Transactional
public void saveFromRow(EntityAuditTableRow auditTableRow) {
Revision revision = revisionsService.load(auditTableRow.getAuditTableRow().getRevision().getId());
EntityChange entityChange = create(auditTableRow);
save(entityChange);
revision.getEntitiesChanges().add(entityChange);
revisionsService.save(revision);
}
答案 0 :(得分:1)
类的事务方法在没有代理的情况下调用此类的其他事务方法,因此第二个方法事务不在invokation中使用。
答案 1 :(得分:0)
要应用@Transactional
并将其打开,必须通过proxy
执行。