org.mybatis.spring
Class SqlSessionUtils

java.lang.Object
  extended by org.mybatis.spring.SqlSessionUtils

public final class SqlSessionUtils
extends Object

Handles MyBatis SqlSession life cycle. It can register and get SqlSessions from Spring TransactionSynchronizationManager. Also works if no transaction is active.

Version:
$Id: SqlSessionUtils.java 3565 2011-01-06 12:00:04Z eduardo.macarron $

Method Summary
static void closeSqlSession(org.apache.ibatis.session.SqlSession session, org.apache.ibatis.session.SqlSessionFactory sessionFactory)
          Checks if SqlSession passed as an argument is managed by Spring TransactionSynchronizationManager 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
static org.apache.ibatis.session.SqlSession getSqlSession(org.apache.ibatis.session.SqlSessionFactory sessionFactory)
          Creates a new MyBatis SqlSession from the SqlSessionFactory provided as a parameter and using its DataSource and ExecutorType
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)
          If a Spring transaction is active it uses DataSourceUtils to get a Spring managed Connection, then creates a new SqlSession with this connection and synchronizes it with the transaction.
static boolean isSqlSessionTransactional(org.apache.ibatis.session.SqlSession session, org.apache.ibatis.session.SqlSessionFactory sessionFactory)
          Returns if the SqlSession passed as an argument is being managed by Spring
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getSqlSession

public static org.apache.ibatis.session.SqlSession getSqlSession(org.apache.ibatis.session.SqlSessionFactory sessionFactory)
Creates a new MyBatis SqlSession from the SqlSessionFactory provided as a parameter and using its DataSource and ExecutorType

Parameters:
sessionFactory - a MyBatis SqlSessionFactory to create new sessions
Returns:
a MyBatis SqlSession
Throws:
org.springframework.dao.TransientDataAccessResourceException - if a transaction is active and the SqlSessionFactory is not using a SpringManagedTransactionFactory

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)
If a Spring transaction is active it uses DataSourceUtils to get a Spring managed Connection, then creates a new SqlSession with this connection and synchronizes it with the transaction. If there is not an active transaction it gets a connection directly from the DataSource and creates a SqlSession with it.

Parameters:
sessionFactory - a MyBatis SqlSessionFactory to create new sessions
executorType - The executor type of the SqlSession to create
exceptionTranslator - Optional. Translates SqlSession.commit() exceptions to Spring exceptions.
Throws:
org.springframework.dao.TransientDataAccessResourceException - if a transaction is active and the SqlSessionFactory is not using a SpringManagedTransactionFactory
See Also:
SpringManagedTransactionFactory

closeSqlSession

public static void closeSqlSession(org.apache.ibatis.session.SqlSession session,
                                   org.apache.ibatis.session.SqlSessionFactory sessionFactory)
Checks if SqlSession passed as an argument is managed by Spring TransactionSynchronizationManager 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 -
sessionFactory -

isSqlSessionTransactional

public static boolean isSqlSessionTransactional(org.apache.ibatis.session.SqlSession session,
                                                org.apache.ibatis.session.SqlSessionFactory sessionFactory)
Returns if the SqlSession passed as an argument is being managed by Spring

Parameters:
session - a MyBatis SqlSession to check
sessionFactory - the SqlSessionFactory which the SqlSession was built with
Returns:
true if session is transactional, otherwise false


Copyright © 2010-2011 MyBatis.org. All Rights Reserved.