title: Cannot import migration data into a MariaDB database
body: <p>I'm trying to import some exported migration data into a <code>MariaDB</code> database.</p>
  
  <p>I could successfully do the import into the <code>H2</code> database.</p>
  
  <p>But when trying to import in the <code>MariaDB</code> one, it creates <code>87</code> tables in the database instead of <code>91</code> tables, and also ends up in error:</p>
  
  <pre><code>2018-04-22 14:13:33,275 INFO  [org.keycloak.connections.jpa.updater.liquibase.LiquibaseJpaUpdaterProvider] (ServerService Thread Pool -- 58) Initializing database schema. Using changelog META-INF/jpa-changelog-master.xml
  2018-04-22 14:18:22,393 ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) WFLYCTL0348: Timeout after [300] seconds waiting for service container stability. Operation will roll back. Step that first updated the service container was 'add' at address '[
      ("core-service" =&gt; "management"),
      ("management-interface" =&gt; "http-interface")
  ]'
  </code></pre>
  
  <p>This new log chunk shows it takes almost 5 mn. It's way too long.</p>
  
  <p>More from the stacktrace:</p>
  
  <pre><code>16:16:55,690 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 58) MSC000001: Failed to start service jboss.undertow.deployment.default-server.default-host./auth: org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./auth: java.lang.RuntimeException: RESTEASY003325: Failed to construct public org.keycloak.services.resources.KeycloakApplication(javax.servlet.ServletContext,org.jboss.resteasy.core.Dispatcher)
      at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:84)
      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
      at java.util.concurrent.FutureTask.run(FutureTask.java:266)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      at java.lang.Thread.run(Thread.java:745)
      at org.jboss.threads.JBossThread.run(JBossThread.java:320)
  Caused by: java.lang.RuntimeException: RESTEASY003325: Failed to construct public org.keycloak.services.resources.KeycloakApplication(javax.servlet.ServletContext,org.jboss.resteasy.core.Dispatcher)
      at org.jboss.resteasy.core.ConstructorInjectorImpl.construct(ConstructorInjectorImpl.java:162)
      at org.jboss.resteasy.spi.ResteasyProviderFactory.createProviderInstance(ResteasyProviderFactory.java:2298)
      at org.jboss.resteasy.spi.ResteasyDeployment.createApplication(ResteasyDeployment.java:340)
      at org.jboss.resteasy.spi.ResteasyDeployment.start(ResteasyDeployment.java:253)
      at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.init(ServletContainerDispatcher.java:120)
      at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.init(HttpServletDispatcher.java:36)
      at io.undertow.servlet.core.LifecyleInterceptorInvocation.proceed(LifecyleInterceptorInvocation.java:117)
      at org.wildfly.extension.undertow.security.RunAsLifecycleInterceptor.init(RunAsLifecycleInterceptor.java:78)
      at io.undertow.servlet.core.LifecyleInterceptorInvocation.proceed(LifecyleInterceptorInvocation.java:103)
      at io.undertow.servlet.core.ManagedServlet$DefaultInstanceStrategy.start(ManagedServlet.java:250)
      at io.undertow.servlet.core.ManagedServlet.createServlet(ManagedServlet.java:133)
      at io.undertow.servlet.core.DeploymentManagerImpl$2.call(DeploymentManagerImpl.java:565)
      at io.undertow.servlet.core.DeploymentManagerImpl$2.call(DeploymentManagerImpl.java:536)
      at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:42)
      at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
      at org.wildfly.extension.undertow.security.SecurityContextThreadSetupAction.lambda$create$0(SecurityContextThreadSetupAction.java:105)
      at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
      at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
      at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
      at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
      at io.undertow.servlet.core.DeploymentManagerImpl.start(DeploymentManagerImpl.java:578)
      at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:100)
      at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:81)
      ... 6 more
  Caused by: java.lang.RuntimeException: Failed to update database
      at org.keycloak.connections.jpa.updater.liquibase.LiquibaseJpaUpdaterProvider.update(LiquibaseJpaUpdaterProvider.java:102)
      at org.keycloak.connections.jpa.updater.liquibase.LiquibaseJpaUpdaterProvider.update(LiquibaseJpaUpdaterProvider.java:67)
      at org.keycloak.connections.jpa.DefaultJpaConnectionProviderFactory.update(DefaultJpaConnectionProviderFactory.java:322)
      at org.keycloak.connections.jpa.DefaultJpaConnectionProviderFactory.migration(DefaultJpaConnectionProviderFactory.java:292)
      at org.keycloak.connections.jpa.DefaultJpaConnectionProviderFactory.lambda$lazyInit$0(DefaultJpaConnectionProviderFactory.java:179)
      at org.keycloak.models.utils.KeycloakModelUtils.suspendJtaTransaction(KeycloakModelUtils.java:544)
      at org.keycloak.connections.jpa.DefaultJpaConnectionProviderFactory.lazyInit(DefaultJpaConnectionProviderFactory.java:130)
      at org.keycloak.connections.jpa.DefaultJpaConnectionProviderFactory.create(DefaultJpaConnectionProviderFactory.java:78)
      at org.keycloak.connections.jpa.DefaultJpaConnectionProviderFactory.create(DefaultJpaConnectionProviderFactory.java:56)
      at org.keycloak.services.DefaultKeycloakSession.getProvider(DefaultKeycloakSession.java:163)
      at org.keycloak.models.jpa.JpaRealmProviderFactory.create(JpaRealmProviderFactory.java:51)
      at org.keycloak.models.jpa.JpaRealmProviderFactory.create(JpaRealmProviderFactory.java:33)
      at org.keycloak.services.DefaultKeycloakSession.getProvider(DefaultKeycloakSession.java:163)
      at org.keycloak.models.cache.infinispan.RealmCacheSession.getDelegate(RealmCacheSession.java:144)
      at org.keycloak.models.cache.infinispan.RealmCacheSession.getMigrationModel(RealmCacheSession.java:137)
      at org.keycloak.migration.MigrationModelManager.migrate(MigrationModelManager.java:76)
      at org.keycloak.services.resources.KeycloakApplication.migrateModel(KeycloakApplication.java:246)
      at org.keycloak.services.resources.KeycloakApplication.migrateAndBootstrap(KeycloakApplication.java:187)
      at org.keycloak.services.resources.KeycloakApplication$1.run(KeycloakApplication.java:146)
      at org.keycloak.models.utils.KeycloakModelUtils.runJobInTransaction(KeycloakModelUtils.java:227)
      at org.keycloak.services.resources.KeycloakApplication.&lt;init&gt;(KeycloakApplication.java:137)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
      at org.jboss.resteasy.core.ConstructorInjectorImpl.construct(ConstructorInjectorImpl.java:150)
      ... 28 more
  Caused by: liquibase.exception.MigrationFailedException: Migration failed for change set META-INF/jpa-changelog-2.1.0.xml::2.1.0::bburke@redhat.com:
       Reason: liquibase.exception.UnexpectedLiquibaseException: java.sql.SQLException: IJ031040: Connection is not associated with a managed connection: org.jboss.jca.adapters.jdbc.jdk8.WrappedConnectionJDK8@55194ba1
      at liquibase.changelog.ChangeSet.execute(ChangeSet.java:573)
      at liquibase.changelog.visitor.UpdateVisitor.visit(UpdateVisitor.java:51)
      at liquibase.changelog.ChangeLogIterator.run(ChangeLogIterator.java:73)
      at liquibase.Liquibase.update(Liquibase.java:210)
      at liquibase.Liquibase.update(Liquibase.java:190)
      at liquibase.Liquibase.update(Liquibase.java:186)
      at org.keycloak.connections.jpa.updater.liquibase.LiquibaseJpaUpdaterProvider.updateChangeSet(LiquibaseJpaUpdaterProvider.java:135)
      at org.keycloak.connections.jpa.updater.liquibase.LiquibaseJpaUpdaterProvider.update(LiquibaseJpaUpdaterProvider.java:88)
      ... 53 more
  Caused by: liquibase.exception.UnexpectedLiquibaseException: java.sql.SQLException: IJ031040: Connection is not associated with a managed connection: org.jboss.jca.adapters.jdbc.jdk8.WrappedConnectionJDK8@55194ba1
      at liquibase.database.jvm.JdbcConnection.getURL(JdbcConnection.java:79)
      at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:62)
      at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:122)
      at liquibase.database.AbstractJdbcDatabase.execute(AbstractJdbcDatabase.java:1247)
      at liquibase.database.AbstractJdbcDatabase.executeStatements(AbstractJdbcDatabase.java:1230)
      at liquibase.changelog.ChangeSet.execute(ChangeSet.java:548)
      ... 60 more
  Caused by: java.sql.SQLException: IJ031040: Connection is not associated with a managed connection: org.jboss.jca.adapters.jdbc.jdk8.WrappedConnectionJDK8@55194ba1
      at org.jboss.jca.adapters.jdbc.WrappedConnection.lock(WrappedConnection.java:164)
      at org.jboss.jca.adapters.jdbc.WrappedConnection.getMetaData(WrappedConnection.java:913)
      at liquibase.database.jvm.JdbcConnection.getURL(JdbcConnection.java:77)
      ... 65 more
  </code></pre>
  
  <p>The export command was:</p>
  
  <pre><code>$KEYCLOAK_HOME/bin/standalone.sh -Dkeycloak.migration.action=export -Dkeycloak.migration.provider=dir -Dkeycloak.migration.dir=exported_realms -Dkeycloak.migration.strategy=OVERWRITE_EXISTING
  </code></pre>
  
  <p>The import command that fails is:</p>
  
  <pre><code>$KEYCLOAK_HOME/bin/standalone.sh -Dkeycloak.migration.action=export -Dkeycloak.migration.provider=dir -Dkeycloak.migration.dir=exported_realms -Dkeycloak.migration.strategy=OVERWRITE_EXISTING
  </code></pre>
  
  <p>Here is the data source used in the <code>standalone/configuration/standalone.xml</code> file:</p>
  
  <pre><code>&lt;datasource jndi-name="java:/jboss/datasources/KeycloakDS" pool-name="KeycloakDS" enabled="true"&gt;
    &lt;connection-url&gt;jdbc:mysql://localhost:3306/keycloak?useSSL=false&amp;amp;characterEncoding=UTF-8&lt;/connection-url&gt;
    &lt;driver&gt;mysql&lt;/driver&gt;
    &lt;pool&gt;
      &lt;min-pool-size&gt;5&lt;/min-pool-size&gt;
      &lt;max-pool-size&gt;15&lt;/max-pool-size&gt;
    &lt;/pool&gt;
    &lt;security&gt;
      &lt;user-name&gt;keycloak&lt;/user-name&gt;
      &lt;password&gt;xxxxxx&lt;/password&gt;
    &lt;/security&gt;
    &lt;validation&gt;
      &lt;valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker"/&gt;
      &lt;validate-on-match&gt;true&lt;/validate-on-match&gt;
      &lt;exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter"/&gt;
    &lt;/validation&gt;
  &lt;/datasource&gt;
  </code></pre>
  
  <p>I'm using <code>keycloak-3.4.1.Final</code> and <code>mariadb-10.1.24</code> on a java version <code>1.8.0_60</code>.</p>
  
  <p>Running the <code>./mysqltuner.pl</code> utility shows:</p>
  
  <pre><code>-------- InnoDB Metrics ----------------------------------------------------------------------------
  [--] InnoDB is enabled.
  [--] InnoDB Thread Concurrency: 0
  [OK] InnoDB File per table is activated
  [OK] InnoDB buffer pool / data size: 2.0G/222.6M
  [OK] Ratio InnoDB log file size / InnoDB Buffer pool size: 256.0M * 2/2.0G should be equal 25%
  [OK] InnoDB buffer pool instances: 2
  [--] InnoDB Buffer Pool Chunk Size not used or defined in your version
  [!!] InnoDB Read buffer efficiency: 63.85% (802 hits/ 1256 total)
  [!!] InnoDB Write Log efficiency: 0% (1 hits/ 0 total)
  [OK] InnoDB log waits: 0.00% (0 waits / 1 writes)
  
  General recommendations:
      Control warning line(s) into /home/stephane/programs/install/mariadb/mariadb.error.log file
      1 CVE(s) found for your MySQL release. Consider upgrading your version !
      MySQL started within last 24 hours - recommendations may be inaccurate
      Dedicate this server to your database for highest performance.
      Reduce or eliminate unclosed connections and network issues
      Consider installing Sys schema from https://github.com/mysql/mysql-sys
  Variables to adjust:
      query_cache_size (=0)
      query_cache_type (=0)
      query_cache_limit (&gt; 1M, or use smaller result sets)
  </code></pre>