MultiTenantQueryBus/AxonServerQueryBus and transactions

Let’s assume that in Spring boot app with axon server and multitenancy extension and some db projections (in postgres db).

There is a rest controller with a few endpoints.

In one example endpoint there is a directly called method from JpaRepository (findBy*). In this case, there is no transaction created on the final query (in Postgres db session view there was a state idle)

In the other example endpoint there is called queryGateway.query method. The handler for this method does exactly the same as previously so calling method from JpaRepository (findBy*). In this case there is a transaction created on the final query (in Postgres db session view there was a state idle in transaction).

Is it possible to configure axon framework (QueryBus?) to not start transactions on queries? I tried to create custom TenantQuerySegmentFactory which creates SimpleQueryBus without transactionManager but it had no affect.

It seams the problem is that bean you are trying to configure is missing @ConditionalOnMissingBean, so your bean is not taken into consideration

Will create ticket and PR shortly

What do you mean? Which bean is missing? I think that TenantQuerySegmentFactory I created is properly used by MultitenantQueryBus when new tenant is registered means new segments with AxonServerQueryBus where created.

When you override TenantQuerySegmentFactory , can you confirm that code inside your TenantQuerySegmentFactory bean is executed?

Yes this code was executed (my bean is Primary) and actually it looks that it is working fine means no transaction interceptor is created so no transaction is started in UoW. I can also see that when I log TransactionSynchronizationManager.isActualTransactionActive() in my handler it returns false means no transaction started.

I am only confused about what I can see in DBeaver postgres Sessions log when I ran some load test.

In first case when run load tests on endpoint with direct JpaRepository method call I can see that queries in Session log with “idle” status

In second case when run load tests on endpoint with query by QueryGateway I can see that queries in Session log with “idle in transaction” status.

I am just curious what affects that.

Forget about it. It is not related to axon framework at all…

Probably some spring/jpa thing… actually there was not exactly the same method called. In first case I called findByIdIn method which doesn’t create transaction and in second case I used findById which creates transaction

1 Like