making snapshots using Hibernate without Spring

Hello,

I’m making a demo project where I’m using event sourcing - events are correctly stored in DomainEventEntry table.
However, I also need to get snapshots running and this is where i’m facing problems.
Snapshots get triggered, but in SnapshotEventEntry table, only the first (root) event is stored , a.k.a ‘…CreatedEvent’, even though the sequence number gets incremented.
Now when i put non-serializable variable to the Aggregate, like entity manager, the snapshotting fails on serialization, but in DB there is a payload with data about the event, although just the 2 events - ‘…CreatedEvent’ and ‘…ChangedEvent’ and sequence number is 1.

I think it might do something with transactions - i dont use Spring, so my transaction looks like this :

public class TransactionManager implements org.axonframework.common.transaction.TransactionManager{

    @Override
    public Transaction startTransaction() {
        return new Transaction();
    }
}

public class Transaction implements org.axonframework.common.transaction.Transaction {

    final EntityManager entityManager;

    public Transaction() {
        this.entityManager = EntityManagerProvider.get();
        if(entityManager.getTransaction().isActive()){
            entityManager.getTransaction().commit();
        }
        entityManager.getTransaction().begin();
    }

    @Override
    public void commit() {
        entityManager.getTransaction().commit();
    }

    @Override
    public void rollback() {
        entityManager.getTransaction().rollback();
    }
}


Hi,

it looks like you’re committing any transactions that are active when starting a nested one. I think it would be better to just keep using any active transaction, instead. That’s how Spring also works with nested transaction boundaries (by default).

How did you configure your snapshotter? I wonder why you’d see the creation event as a snapshot. Normally, a snapshot consists of the serialize state of the aggregate itself.

Cheers,

Allard