Package org.mybatis.spring
Class SqlSessionUtils
- java.lang.Object
-
- org.mybatis.spring.SqlSessionUtils
-
public final class SqlSessionUtils extends java.lang.Object
Handles MyBatis SqlSession life cycle. It can register and get SqlSessions from SpringTransactionSynchronizationManager
. Also works if no transaction is active.- Author:
- Hunter Presnall, Eduardo Macarron
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
closeSqlSession(org.apache.ibatis.session.SqlSession session, org.apache.ibatis.session.SqlSessionFactory sessionFactory)
Checks ifSqlSession
passed as an argument is managed by SpringTransactionSynchronizationManager
If it is not, it closes it, otherwise it just updates the reference counter and lets Spring call the close callback when the managed transaction endsstatic org.apache.ibatis.session.SqlSession
getSqlSession(org.apache.ibatis.session.SqlSessionFactory sessionFactory)
Creates a new MyBatisSqlSession
from theSqlSessionFactory
provided as a parameter and using itsDataSource
andExecutorType
static org.apache.ibatis.session.SqlSession
getSqlSession(org.apache.ibatis.session.SqlSessionFactory sessionFactory, org.apache.ibatis.session.ExecutorType executorType, org.springframework.dao.support.PersistenceExceptionTranslator exceptionTranslator)
Gets an SqlSession from Spring Transaction Manager or creates a new one if needed.static boolean
isSqlSessionTransactional(org.apache.ibatis.session.SqlSession session, org.apache.ibatis.session.SqlSessionFactory sessionFactory)
Returns if theSqlSession
passed as an argument is being managed by Spring
-
-
-
Method Detail
-
getSqlSession
public static org.apache.ibatis.session.SqlSession getSqlSession(org.apache.ibatis.session.SqlSessionFactory sessionFactory)
Creates a new MyBatisSqlSession
from theSqlSessionFactory
provided as a parameter and using itsDataSource
andExecutorType
- Parameters:
sessionFactory
- a MyBatisSqlSessionFactory
to create new sessions- Returns:
- a MyBatis
SqlSession
- Throws:
org.springframework.dao.TransientDataAccessResourceException
- if a transaction is active and theSqlSessionFactory
is not using aSpringManagedTransactionFactory
-
getSqlSession
public static org.apache.ibatis.session.SqlSession getSqlSession(org.apache.ibatis.session.SqlSessionFactory sessionFactory, org.apache.ibatis.session.ExecutorType executorType, org.springframework.dao.support.PersistenceExceptionTranslator exceptionTranslator)
Gets an SqlSession from Spring Transaction Manager or creates a new one if needed. Tries to get a SqlSession out of current transaction. If there is not any, it creates a new one. Then, it synchronizes the SqlSession with the transaction if Spring TX is active andSpringManagedTransactionFactory
is configured as a transaction manager.- Parameters:
sessionFactory
- a MyBatisSqlSessionFactory
to create new sessionsexecutorType
- The executor type of the SqlSession to createexceptionTranslator
- Optional. Translates SqlSession.commit() exceptions to Spring exceptions.- Returns:
- an SqlSession managed by Spring Transaction Manager
- Throws:
org.springframework.dao.TransientDataAccessResourceException
- if a transaction is active and theSqlSessionFactory
is not using aSpringManagedTransactionFactory
- See Also:
SpringManagedTransactionFactory
-
closeSqlSession
public static void closeSqlSession(org.apache.ibatis.session.SqlSession session, org.apache.ibatis.session.SqlSessionFactory sessionFactory)
Checks ifSqlSession
passed as an argument is managed by SpringTransactionSynchronizationManager
If it is not, it closes it, otherwise it just updates the reference counter and lets Spring call the close callback when the managed transaction ends- Parameters:
session
- a target SqlSessionsessionFactory
- a factory of SqlSession
-
isSqlSessionTransactional
public static boolean isSqlSessionTransactional(org.apache.ibatis.session.SqlSession session, org.apache.ibatis.session.SqlSessionFactory sessionFactory)
Returns if theSqlSession
passed as an argument is being managed by Spring- Parameters:
session
- a MyBatis SqlSession to checksessionFactory
- the SqlSessionFactory which the SqlSession was built with- Returns:
- true if session is transactional, otherwise false
-
-