В коде вызываю события транзакции:
int insertCatalog(Catalog catalog)
{
publisher.publishEvent(new TransactionEvent("MyRepository.insertCatalog()", "1"));
String sql = "insert into catalogs(name, parent, image) values(?,?,?)";
KeyHolder keyHolder = new GeneratedKeyHolder();
jdbcTemplate.update(con->
{
PreparedStatement ps = con.prepareStatement(sql, new String []{"id"});
ps.setString(1, catalog.getName());
if(!catalog.getParent().isEmpty())
ps.setInt(2, catalog.getParent().get().getId());
else
ps.setNull(2, Types.NULL);
if(catalog.getImage().isEmpty())
ps.setNull(3, Types.NULL);
else
ps.setBytes(3, catalog.getImage().get());
return ps;
}, keyHolder);
publisher.publishEvent(new TransactionEvent("MyRepository.insertCatalog()", "2"));
return keyHolder.getKey().intValue();
}
Настройки транзакции для класса, содержащего метод insertCatalog(..)
@Transactional(propagation = Propagation.REQUIRED,
isolation = Isolation.REPEATABLE_READ,
rollbackFor = {RollbackDatabaseException.class},
timeout = 5)
Интересное в логах:
15-Jan-2024 13:52:58.958 INFO [https-jsse-nio-8443-exec-18] mainPackage.Classes.Events.TransactionEventListener.handleEventBeforeCommit MyRepository.insertCatalog():1 - BEFORE_COMMIT
15-Jan-2024 13:52:58.960 INFO [https-jsse-nio-8443-exec-18] mainPackage.Classes.Events.TransactionEventListener.handleEventBeforeCommit MyRepository.insertCatalog():1 - BEFORE_COMMIT
15-Jan-2024 13:52:58.960 INFO [https-jsse-nio-8443-exec-18] mainPackage.Classes.Events.TransactionEventListener.handleEventBeforeCommit MyRepository.insertCatalog():2 - BEFORE_COMMIT
15-Jan-2024 13:52:58.960 INFO [https-jsse-nio-8443-exec-18] mainPackage.Classes.Events.TransactionEventListener.handleEventBeforeCommit MyRepository.insertCatalog():2 - BEFORE_COMMIT
15-Jan-2024 13:52:58.966 INFO [https-jsse-nio-8443-exec-18] mainPackage.Classes.Events.TransactionEventListener.handleEventAfterCommit MyRepository.insertCatalog():1 - AFTER_COMMIT
15-Jan-2024 13:52:58.967 INFO [https-jsse-nio-8443-exec-18] mainPackage.Classes.Events.TransactionEventListener.handleEventAfterCompletion MyRepository.insertCatalog():1 - AFTER_COMPLETION
15-Jan-2024 13:52:58.968 INFO [https-jsse-nio-8443-exec-18] mainPackage.Classes.Events.TransactionEventListener.handleEventAfterCommit MyRepository.insertCatalog():1 - AFTER_COMMIT
15-Jan-2024 13:52:58.968 INFO [https-jsse-nio-8443-exec-18] mainPackage.Classes.Events.TransactionEventListener.handleEventAfterCompletion MyRepository.insertCatalog():1 - AFTER_COMPLETION
15-Jan-2024 13:52:58.968 INFO [https-jsse-nio-8443-exec-18] mainPackage.Classes.Events.TransactionEventListener.handleEventAfterCommit MyRepository.insertCatalog():2 - AFTER_COMMIT
15-Jan-2024 13:52:58.968 INFO [https-jsse-nio-8443-exec-18] mainPackage.Classes.Events.TransactionEventListener.handleEventAfterCompletion MyRepository.insertCatalog():2 - AFTER_COMPLETION
15-Jan-2024 13:52:58.968 INFO [https-jsse-nio-8443-exec-18] mainPackage.Classes.Events.TransactionEventListener.handleEventAfterCommit MyRepository.insertCatalog():2 - AFTER_COMMIT
15-Jan-2024 13:52:58.969 INFO [https-jsse-nio-8443-exec-18] mainPackage.Classes.Events.TransactionEventListener.handleEventAfterCompletion MyRepository.insertCatalog():2 - AFTER_COMPLETION
Почему событие несколько раз срабатывает в одной точке для одной и той же фазы?