Skip to content

SqlSessionTemplate bean is not valid for disposal by Spring container (https://github.com/mybatis/spring/issues/77) #78

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 2 commits into from
Aug 23, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/main/java/org/mybatis/spring/SqlSessionTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.dao.support.PersistenceExceptionTranslator;

/**
Expand Down Expand Up @@ -69,12 +70,12 @@
* @author Putthibong Boonbong
* @author Hunter Presnall
* @author Eduardo Macarron
*
*
* @see SqlSessionFactory
* @see MyBatisExceptionTranslator
* @version $Id$
*/
public class SqlSessionTemplate implements SqlSession {
public class SqlSessionTemplate implements SqlSession, DisposableBean {

private final SqlSessionFactory sqlSessionFactory;

Expand Down Expand Up @@ -395,6 +396,28 @@ public List<BatchResult> flushStatements() {
}

/**
* Allow gently dispose bean:
* <pre>
* {@code
*
* <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
* <constructor-arg index="0" ref="sqlSessionFactory" />
* </bean>
* }
*</pre>
*
* The implementation of {@link DisposableBean} forces spring context to use {@link DisposableBean#destroy()} method instead of {@link SqlSessionTemplate#close()} to shutdown gently.
*
* @see SqlSessionTemplate#close()
* @see org.springframework.beans.factory.support.DisposableBeanAdapter#inferDestroyMethodIfNecessary
* @see org.springframework.beans.factory.support.DisposableBeanAdapter#CLOSE_METHOD_NAME
*/
@Override
public void destroy() throws Exception {
//This method forces spring disposer to avoid call of SqlSessionTemplate.close() which gives UnsupportedOperationException
}

/**
* Proxy needed to route MyBatis method calls to the proper SqlSession got
* from Spring's Transaction Manager
* It also unwraps exceptions thrown by {@code Method#invoke(Object, Object...)} to
Expand Down