Repository: quarkusio/quarkus
Issue: Unused Narayana is started upon application start

### Describe the bug

`mongodb-panache-common` adds the `narayana-jta` extension. Although added, the transaction manager was never used and thus not started. 

As of `2.13.0.CR1` the transaction manager is started nearly immediately at startup


### Expected behavior

Narayana is not started if not used.

Additionally, it would be nice to not have it included if it's not going to be used.

### Actual behavior

Narayana JTA is started almost immediately on startup.

In an application with Flyway enabled, the JTA is started prior to running migrations and a message is produced.
```
20:29:15 INFO  ARJUNA032010: JBossTS Recovery Service (tag: 518c8dd50a9ae8e70eb15dfe8fc764adcabef8ab) - JBoss Inc.
20:29:15 INFO  ARJUNA032013: Starting transaction recovery manager
20:29:15 INFO  Flyway Community Edition 9.3.0 by Redgate
```


In applications without Flyway **no message is produced** but the application creates the telltale `ObjectStore` directory indicating that some portion of the JTA is starting.


### How to Reproduce?

Add `mongodb-panache` to an application to you'll see `ObjectStore` directories.

Additionally add Flyway and observe the logging related to started the JTA.


### Output of `uname -a` or `ver`

macOS 12.6

### Output of `java -version`

OpenJDK 17.0.1

### GraalVM version (if different from Java)

_No response_

### Quarkus version or git rev

2.13.0.CR1

### Build tool (ie. output of `mvnw --version` or `gradlew --version`)

Gradle 7.5.1

### Additional information

_No response_

Changed files:
extensions/narayana-jta/runtime/src/main/java/io/quarkus/narayana/jta/runtime/NarayanaJtaRecorder.java
extensions/narayana-jta/runtime/src/main/java/io/quarkus/narayana/jta/runtime/TransactionManagerConfiguration.java
extensions/narayana-jta/runtime/src/main/java/io/quarkus/narayana/jta/runtime/NarayanaJtaProducers.java
extensions/agroal/runtime/src/main/java/io/quarkus/agroal/runtime/DataSources.java
extensions/narayana-jta/deployment/src/main/java/io/quarkus/narayana/jta/deployment/NarayanaJtaProcessor.java

diff --git a/extensions/agroal/runtime/src/main/java/io/quarkus/agroal/runtime/DataSources.java b/extensions/agroal/runtime/src/main/java/io/quarkus/agroal/runtime/DataSources.java
index 9adbe0abf06..28172208ff0 100644
--- a/extensions/agroal/runtime/src/main/java/io/quarkus/agroal/runtime/DataSources.java
+++ b/extensions/agroal/runtime/src/main/java/io/quarkus/agroal/runtime/DataSources.java
@@ -49,6 +49,7 @@
 import io.quarkus.datasource.runtime.DataSourceRuntimeConfig;
 import io.quarkus.datasource.runtime.DataSourcesBuildTimeConfig;
 import io.quarkus.datasource.runtime.DataSourcesRuntimeConfig;
+import io.quarkus.narayana.jta.runtime.TransactionManagerConfiguration;
 
 /**
  * This class is sort of a producer for {@link AgroalDataSource}.
@@ -72,6 +73,7 @@ public class DataSources {
     private final DataSourcesRuntimeConfig dataSourcesRuntimeConfig;
     private final DataSourcesJdbcBuildTimeConfig dataSourcesJdbcBuildTimeConfig;
     private final DataSourcesJdbcRuntimeConfig dataSourcesJdbcRuntimeConfig;
+    private final TransactionManagerConfiguration transactionRuntimeConfig;
     private final TransactionManager transactionManager;
     private final XAResourceRecoveryRegistry xaResourceRecoveryRegistry;
     private final TransactionSynchronizationRegistry transactionSynchronizationRegistry;
@@ -83,6 +85,7 @@ public class DataSources {
     public DataSources(DataSourcesBuildTimeConfig dataSourcesBuildTimeConfig,
             DataSourcesRuntimeConfig dataSourcesRuntimeConfig, DataSourcesJdbcBuildTimeConfig dataSourcesJdbcBuildTimeConfig,
             DataSourcesJdbcRuntimeConfig dataSourcesJdbcRuntimeConfig,
+            TransactionManagerConfiguration transactionRuntimeConfig,
             TransactionManager transactionManager,
             XAResourceRecoveryRegistry xaResourceRecoveryRegistry,
             TransactionSynchronizationRegistry transactionSynchronizationRegistry, DataSourceSupport dataSourceSupport,
@@ -91,6 +94,7 @@ public DataSources(DataSourcesBuildTimeConfig dataSourcesBuildTimeConfig,
         this.dataSourcesRuntimeConfig = dataSourcesRuntimeConfig;
         this.dataSourcesJdbcBuildTimeConfig = dataSourcesJdbcBuildTimeConfig;
         this.dataSourcesJdbcRuntimeConfig = dataSourcesJdbcRuntimeConfig;
+        this.transactionRuntimeConfig = transactionRuntimeConfig;
         this.transactionManager = transactionManager;
         this.xaResourceRecoveryRegistry = xaResourceRecoveryRegistry;
         this.transactionSynchronizationRegistry = transactionSynchronizationRegistry;
@@ -215,8 +219,10 @@ public AgroalDataSource doCreateDataSource(String dataSourceName) {
                 .connectionFactoryConfiguration();
 
         boolean mpMetricsPresent = dataSourceSupport.mpMetricsPresent;
-        applyNewConfiguration(dataSourceConfiguration, poolConfiguration, connectionFactoryConfiguration, driver, jdbcUrl,
-                dataSourceJdbcBuildTimeConfig, dataSourceRuntimeConfig, dataSourceJdbcRuntimeConfig, mpMetricsPresent);
+        applyNewConfiguration(dataSourceName, dataSourceConfiguration, poolConfiguration, connectionFactoryConfiguration,
+                driver, jdbcUrl,
+                dataSourceJdbcBuildTimeConfig, dataSourceRuntimeConfig, dataSourceJdbcRuntimeConfig, transactionRuntimeConfig,
+                mpMetricsPresent);
 
         if (dataSourceSupport.disableSslSupport) {
             agroalConnectionConfigurer.disableSslSupport(resolvedDbKind, dataSourceConfiguration);
@@ -255,11 +261,12 @@ public AgroalDataSource doCreateDataSource(String dataSourceName) {
         return dataSource;
     }
 
-    private void applyNewConfiguration(AgroalDataSourceConfigurationSupplier dataSourceConfiguration,
+    private void applyNewConfiguration(String dataSourceName, AgroalDataSourceConfigurationSupplier dataSourceConfiguration,
             AgroalConnectionPoolConfigurationSupplier poolConfiguration,
             AgroalConnectionFactoryConfigurationSupplier connectionFactoryConfiguration, Class<?> driver, String jdbcUrl,
             DataSourceJdbcBuildTimeConfig dataSourceJdbcBuildTimeConfig, DataSourceRuntimeConfig dataSourceRuntimeConfig,
-            DataSourceJdbcRuntimeConfig dataSourceJdbcRuntimeConfig, boolean mpMetricsPresent) {
+            DataSourceJdbcRuntimeConfig dataSourceJdbcRuntimeConfig, TransactionManagerConfiguration transactionRuntimeConfig,
+            boolean mpMetricsPresent) {
         connectionFactoryConfiguration.jdbcUrl(jdbcUrl);
         connectionFactoryConfiguration.connectionProviderClass(driver);
         connectionFactoryConfiguration.trackJdbcResources(dataSourceJdbcRuntimeConfig.detectStatementLeaks);
@@ -274,8 +281,15 @@ private void applyNewConfiguration(AgroalDataSourceConfigurationSupplier dataSou
             TransactionIntegration txIntegration = new NarayanaTransactionIntegration(transactionManager,
                     transactionSynchronizationRegistry, null, false,
                     dataSourceJdbcBuildTimeConfig.transactions == io.quarkus.agroal.runtime.TransactionIntegration.XA
-                            ? xaResourceRecoveryRegistry
-                            : null);
+                            && transactionRuntimeConfig.enableRecovery
+                                    ? xaResourceRecoveryRegistry
+                                    : null);
+            if (dataSourceJdbcBuildTimeConfig.transactions == io.quarkus.agroal.runtime.TransactionIntegration.XA
+                    && !transactionRuntimeConfig.enableRecovery) {
+                log.warnv(
+                        "Datasource {0} enables XA but transaction recovery is not enabled. Please enable transaction recovery by setting quarkus.transaction-manager.enable-recovery=true, otherwise data may be lost if the application is terminated abruptly",
+                        dataSourceName);
+            }
             poolConfiguration.transactionIntegration(txIntegration);
         }
 
diff --git a/extensions/narayana-jta/deployment/src/main/java/io/quarkus/narayana/jta/deployment/NarayanaJtaProcessor.java b/extensions/narayana-jta/deployment/src/main/java/io/quarkus/narayana/jta/deployment/NarayanaJtaProcessor.java
index abd0b310441..2ca5d25e337 100644
--- a/extensions/narayana-jta/deployment/src/main/java/io/quarkus/narayana/jta/deployment/NarayanaJtaProcessor.java
+++ b/extensions/narayana-jta/deployment/src/main/java/io/quarkus/narayana/jta/deployment/NarayanaJtaProcessor.java
@@ -82,7 +82,7 @@ public void build(NarayanaJtaRecorder recorder,
             BuildProducer<RuntimeInitializedClassBuildItem> runtimeInit,
             BuildProducer<FeatureBuildItem> feature,
             TransactionManagerConfiguration transactions, ShutdownContextBuildItem shutdownContextBuildItem) {
-        recorder.handleShutdown(shutdownContextBuildItem);
+        recorder.handleShutdown(shutdownContextBuildItem, transactions);
         feature.produce(new FeatureBuildItem(Feature.NARAYANA_JTA));
         additionalBeans.produce(new AdditionalBeanBuildItem(NarayanaJtaProducers.class));
         additionalBeans.produce(new AdditionalBeanBuildItem(CDIDelegatingTransactionManager.class));
diff --git a/extensions/narayana-jta/runtime/src/main/java/io/quarkus/narayana/jta/runtime/NarayanaJtaProducers.java b/extensions/narayana-jta/runtime/src/main/java/io/quarkus/narayana/jta/runtime/NarayanaJtaProducers.java
index abfb7d0d36f..77bbaa4f0f2 100644
--- a/extensions/narayana-jta/runtime/src/main/java/io/quarkus/narayana/jta/runtime/NarayanaJtaProducers.java
+++ b/extensions/narayana-jta/runtime/src/main/java/io/quarkus/narayana/jta/runtime/NarayanaJtaProducers.java
@@ -34,10 +34,12 @@ public javax.transaction.UserTransaction userTransaction() {
 
     @Produces
     @Singleton
-    public XAResourceRecoveryRegistry xaResourceRecoveryRegistry() {
+    public XAResourceRecoveryRegistry xaResourceRecoveryRegistry(TransactionManagerConfiguration config) {
         RecoveryManagerService recoveryManagerService = new RecoveryManagerService();
-        recoveryManagerService.create();
-        recoveryManagerService.start();
+        if (config.enableRecovery) {
+            recoveryManagerService.create();
+            recoveryManagerService.start();
+        }
         return recoveryManagerService;
     }
 
diff --git a/extensions/narayana-jta/runtime/src/main/java/io/quarkus/narayana/jta/runtime/NarayanaJtaRecorder.java b/extensions/narayana-jta/runtime/src/main/java/io/quarkus/narayana/jta/runtime/NarayanaJtaRecorder.java
index 82d2e61b95e..ac859f35023 100644
--- a/extensions/narayana-jta/runtime/src/main/java/io/quarkus/narayana/jta/runtime/NarayanaJtaRecorder.java
+++ b/extensions/narayana-jta/runtime/src/main/java/io/quarkus/narayana/jta/runtime/NarayanaJtaRecorder.java
@@ -86,11 +86,13 @@ public void setConfig(final TransactionManagerConfiguration transactions) {
                 .setXaResourceOrphanFilterClassNames(transactions.xaResourceOrphanFilters);
     }
 
-    public void handleShutdown(ShutdownContext context) {
+    public void handleShutdown(ShutdownContext context, TransactionManagerConfiguration transactions) {
         context.addLastShutdownTask(new Runnable() {
             @Override
             public void run() {
-                RecoveryManager.manager().terminate(true);
+                if (transactions.enableRecovery) {
+                    RecoveryManager.manager().terminate(true);
+                }
                 TransactionReaper.terminate(false);
             }
         });
diff --git a/extensions/narayana-jta/runtime/src/main/java/io/quarkus/narayana/jta/runtime/TransactionManagerConfiguration.java b/extensions/narayana-jta/runtime/src/main/java/io/quarkus/narayana/jta/runtime/TransactionManagerConfiguration.java
index b4f8ab1ee8c..be182bd3d76 100644
--- a/extensions/narayana-jta/runtime/src/main/java/io/quarkus/narayana/jta/runtime/TransactionManagerConfiguration.java
+++ b/extensions/narayana-jta/runtime/src/main/java/io/quarkus/narayana/jta/runtime/TransactionManagerConfiguration.java
@@ -32,6 +32,12 @@ public final class TransactionManagerConfiguration {
     @ConfigItem(defaultValue = "ObjectStore")
     public String objectStoreDirectory;
 
+    /**
+     * Start the recovery service on startup.
+     */
+    @ConfigItem(defaultValue = "false")
+    public boolean enableRecovery;
+
     /**
      * The list of recovery modules
      */