Skip to content

Allow to configure the SQLExceptionTranslator on MyBatisExceptionTranslator #401

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 11, 2019

Conversation

kazuki43zoo
Copy link
Member

Fixes gh-400

@evfool
Copy link

evfool commented Sep 9, 2019

Hmmm... in our implementation I have added another constructor arg for the exceptiontranslator, however that doesnt work well with the lazy init. I would've created a PR with that tomorrow. Not sure how suppliers could be used in a Spring xml config, I think the Springy thing is JSR 330 Provider.

@kazuki43zoo
Copy link
Member Author

This change is easy to use together with Java Config such as follows(recommended usage):

  @Bean
  public SqlSessionTemplate sqlSessionTemplate() throws Exception {
    return new SqlSessionTemplate(sqlSessionFactory(), ExecutorType.SIMPLE,
      new MyBatisExceptionTranslator(SQLExceptionSubclassTranslator::new, true));
  }

If you use xml configuration, I propose to make a helper method as follow:

public class MyBatisConfigurationHelper {
  public static Supplier<SQLExceptionTranslator> newSQLExceptionTranslatorSupplier() {
    return SQLExceptionSubclassTranslator::new;
  }
}

and call it on xml configuration as follow:

  <bean id="customSqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
    <constructor-arg name="executorType" value="SIMPLE"/>
    <constructor-arg name="exceptionTranslator">
      <bean class="org.mybatis.spring.MyBatisExceptionTranslator">
        <constructor-arg>
          <bean class="org.mybatis.spring.sample.config.MyBatisConfigurationHelper"
                factory-method="newSQLExceptionTranslatorSupplier"/>
        </constructor-arg>
        <constructor-arg name="exceptionTranslatorLazyInit" value="true"/>
      </bean>
    </constructor-arg>
  </bean>

@evfool
Copy link

evfool commented Sep 10, 2019

Indeed, factory-method should work. Thanks for the hint. The changeset looks fine then, easy to use, and would be fine to have in the next release (not sure if there will be another 1.3.x version or only 2.x.y). I'd be grateful if it would get into the 1.3 branch, but if not, I'll backport it myself locally. Thank you for your quick help and responsiveness.

@kazuki43zoo kazuki43zoo merged commit 9bd7b03 into mybatis:master Sep 11, 2019
@kazuki43zoo kazuki43zoo deleted the gh-400 branch September 11, 2019 22:26
@kazuki43zoo
Copy link
Member Author

@evfool

I've merged this to the master branch(2.0.x). But there is no plan that backport this change to the 1.3.x because this change is no critical bug and required with Java 8(1.3.x support Java 6 and 7 too). If you need a new feature, please consider for updating to latest version.

Thanks again for your contribution!

@evfool
Copy link

evfool commented Sep 12, 2019

@kazuki43zoo thanks for fixing this again. I'm planning to update to the latest version, but have a few more blockers with the legacy code I'm dealing with.

pulllock pushed a commit to pulllock/mybatis-spring that referenced this pull request Oct 19, 2023
Allow to configure the SQLExceptionTranslator on MyBatisExceptionTranslator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow the use of an existing SQLExceptionTranslator
2 participants