|
| 1 | +package io.quarkus.agroal.test; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 6 | + |
| 7 | +import java.sql.SQLException; |
| 8 | + |
| 9 | +import jakarta.enterprise.context.control.ActivateRequestContext; |
| 10 | +import jakarta.inject.Inject; |
| 11 | + |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 14 | + |
| 15 | +import io.agroal.api.AgroalDataSource; |
| 16 | +import io.agroal.api.configuration.AgroalConnectionPoolConfiguration; |
| 17 | +import io.agroal.narayana.NarayanaTransactionIntegration; |
| 18 | +import io.quarkus.agroal.DataSource; |
| 19 | +import io.quarkus.narayana.jta.QuarkusTransaction; |
| 20 | +import io.quarkus.test.QuarkusUnitTest; |
| 21 | + |
| 22 | +public class XaDataSourceConfigTest { |
| 23 | + |
| 24 | + //tag::injection[] |
| 25 | + @Inject |
| 26 | + @DataSource("xa") |
| 27 | + AgroalDataSource xaRecoverDS; |
| 28 | + |
| 29 | + @Inject |
| 30 | + @DataSource("xaNoRecover") |
| 31 | + AgroalDataSource xaNoRecoverDS; |
| 32 | + //end::injection[] |
| 33 | + |
| 34 | + @RegisterExtension |
| 35 | + static final QuarkusUnitTest config = new QuarkusUnitTest() |
| 36 | + .withConfigurationResource("application-recovery-datasources.properties"); |
| 37 | + |
| 38 | + @Test |
| 39 | + @ActivateRequestContext |
| 40 | + public void testEnlistDatasourcesWithRecovery() throws SQLException { |
| 41 | + AgroalConnectionPoolConfiguration xaRecoverConfig = xaRecoverDS.getConfiguration().connectionPoolConfiguration(); |
| 42 | + AgroalConnectionPoolConfiguration xaNoRecoverConfig = xaNoRecoverDS.getConfiguration().connectionPoolConfiguration(); |
| 43 | + |
| 44 | + assertTrue(xaRecoverConfig.recoveryEnable(), "xaRecoverDS datasource should have recover enabled"); |
| 45 | + assertFalse(xaNoRecoverConfig.recoveryEnable(), "xaNoRecoverDS datasource should not have recover enabled"); |
| 46 | + |
| 47 | + assertInstanceOf(NarayanaTransactionIntegration.class, xaRecoverConfig.transactionIntegration(), |
| 48 | + "Agroal transaction integration should use Narayana for xaRecoverDS"); |
| 49 | + assertInstanceOf(NarayanaTransactionIntegration.class, xaNoRecoverConfig.transactionIntegration(), |
| 50 | + "Agroal transaction integration should use Narayana for xaNoRecoverDS"); |
| 51 | + |
| 52 | + // run a transaction and use the two datasources, ensuring that it commits ok |
| 53 | + QuarkusTransaction.begin(); |
| 54 | + |
| 55 | + // Remark: the two datasources will have been registered with the transaction recovery system because the config |
| 56 | + // includes quarkus.transaction-manager.enable-recovery=true |
| 57 | + // see QuarkusRecoveryService for details of how the recovery service manages connections to datasources |
| 58 | + try (var conn = xaRecoverDS.getConnection()) { |
| 59 | + assertFalse(conn.getAutoCommit(), "XA connection should not have the auto commit flag set"); |
| 60 | + try (var conn2 = xaNoRecoverDS.getConnection()) { |
| 61 | + assertFalse(conn2.getAutoCommit(), "XA connection should not have the auto commit flag set"); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + assertTrue(QuarkusTransaction.isActive(), "transaction should still have been active"); |
| 66 | + |
| 67 | + QuarkusTransaction.commit(); |
| 68 | + } |
| 69 | +} |
0 commit comments