/**
 * Copyright 2010-2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.mybatis.spring;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.apache.ibatis.exceptions.PersistenceException;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.support.PersistenceExceptionTranslator;
import org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator;
import org.springframework.jdbc.support.SQLExceptionTranslator;
import org.springframework.transaction.TransactionException;

/**
 * Default exception translator.
 *
 * Translates MyBatis SqlSession returned exception into a Spring {@code DataAccessException} using Spring's
 * {@code SQLExceptionTranslator} Can load {@code SQLExceptionTranslator} eagerly or when the first exception is
 * translated.
 *
 * @author Eduardo Macarron
 */
public class MyBatisExceptionTranslator implements PersistenceExceptionTranslator {

  private final DataSource dataSource;

  private SQLExceptionTranslator exceptionTranslator;

  /**
   * Creates a new {@code DataAccessExceptionTranslator} instance.
   *
   * @param dataSource
   *          DataSource to use to find metadata and establish which error codes are usable.
   * @param exceptionTranslatorLazyInit
   *          if true, the translator instantiates internal stuff only the first time will have the need to translate
   *          exceptions.
   */
  public MyBatisExceptionTranslator(DataSource dataSource, boolean exceptionTranslatorLazyInit) {
    this.dataSource = dataSource;

    if (!exceptionTranslatorLazyInit) {
      this.initExceptionTranslator();
    }
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public DataAccessException translateExceptionIfPossible(RuntimeException e) {
    if (e instanceof PersistenceException) {
      // Batch exceptions come inside another PersistenceException
      // recursion has a risk of infinite loop so better make another if
      if (e.getCause() instanceof PersistenceException) {
        e = (PersistenceException) e.getCause();
      }
      if (e.getCause() instanceof SQLException) {
        this.initExceptionTranslator();
        return this.exceptionTranslator.translate(e.getMessage() + "\n", null, (SQLException) e.getCause());
      } else if (e.getCause() instanceof TransactionException) {
        throw (TransactionException) e.getCause();
      }
      return new MyBatisSystemException(e);
    }
    return null;
  }

  /**
   * Initializes the internal translator reference.
   */
  private synchronized void initExceptionTranslator() {
    if (this.exceptionTranslator == null) {
      this.exceptionTranslator = new SQLErrorCodeSQLExceptionTranslator(this.dataSource);
    }
  }

}

@@ -671,16 +671,52 @@
 ception;
+%0Aimport java.util.function.Supplier;
 %0A%0Aimport
@@ -1539,31 +1539,69 @@
 nal 
-DataSource dataSource;%0A
+Supplier%3CSQLExceptionTranslator%3E exceptionTranslatorSupplier;
 %0A  p
@@ -1680,34 +1680,35 @@
  new %7B@code 
-DataAccess
+Persistence
 ExceptionTra
@@ -1724,16 +1724,64 @@
 instance
+ with %7B@code SQLErrorCodeSQLExceptionTranslator%7D
 .%0A   *%0A 
@@ -2195,34 +2195,763 @@
 this
-.dataSource = dataSource;%0A
+(() -%3E new SQLErrorCodeSQLExceptionTranslator(dataSource), exceptionTranslatorLazyInit);%0A  %7D%0A%0A  /**%0A   * Creates a new %7B@code PersistenceExceptionTranslator%7D instance with specified %7B@code SQLExceptionTranslator%7D.%0A   *%0A   * @param exceptionTranslatorSupplier%0A   *          Supplier for creating a %7B@code SQLExceptionTranslator%7D instance%0A   * @param exceptionTranslatorLazyInit%0A   *          if true, the translator instantiates internal stuff only the first time will have the need to translate%0A   *          exceptions.%0A   * @since 2.0.3%0A   */%0A  public MyBatisExceptionTranslator(Supplier%3CSQLExceptionTranslator%3E exceptionTranslatorSupplier,%0A      boolean exceptionTranslatorLazyInit) %7B%0A    this.exceptionTranslatorSupplier = exceptionTranslatorSupplier;
 %0A   
@@ -4051,36 +4051,17 @@
 lator = 
-new SQLErrorCodeSQLE
+e
 xception
@@ -4074,24 +4074,21 @@
 ator
-(this.dataSource
+Supplier.get(
 );%0A 

Allow to configure the SQLExceptionTranslator on MyBatisExceptionTranslator Fixes gh-400