Home > Back-end >  Learning to mybatis recently discovered a problem, from the threadlocal sqlsession is null is null
Learning to mybatis recently discovered a problem, from the threadlocal sqlsession is null is null

Time:01-19

SqlSessionUtil tools
 public class SqlsessionUtil {
Private static SqlSessionFactory SqlSessionFactory;
Private static ThreadLocal ThreadLocal;
//class loading initialization sqlSessionFactory
The static {
Try {
InputStream InputStream=Resources. GetResourceAsStream (" mybatis - config. XML ");
SqlSessionFactory=new the SqlSessionFactoryBuilder is (). The build (inputStream);
} the catch (IOException e) {
e.printStackTrace();
}
ThreadLocal=new ThreadLocal (a);
}

/* *
* get the sqlsession
* @ return
*/
Public static SqlSession getSqlCurrentSqlsession () {
SqlSession SqlSession=threadLocal. The get ();
If (sqlSession==null) {
SqlSession=sqlSessionFactory. OpenSession ();
ThreadLocal. Set (sqlSession);
}
Return the sqlSession;
}

/* *
* rollback
* @ param sqlSession
*/
Public static void rollBack (SqlSession SqlSession) {

If (sqlSession!=null) {
sqlSession.rollback();
}
}

/* *
* release resources
* @ param sqlSession
*/
Public static void close (SqlSession SqlSession) {
If (sqlSession!=null) {
SqlSession. Close ();
ThreadLocal. Remove ();
}

}


}


The transaction processor TransactionHandler
 
Public class TransactionHandler implements InvocationHandler {
//target
private Object target;
//the constructor

Public TransactionHandler Object (target) {
this.target=target;
}
//get a proxy object
Public Object getProxy () {
Return Proxy newProxyInstance (target. GetClass () getClassLoader (), target. GetClass () getInterfaces (), this);
}

@ Override
Public Object invoke (Object proxy, Method Method, the Object [] args) throws Throwable {
SqlSession SqlSession=null;
The Object invoke=null;
Try {
//manual commit the transaction
SqlSession=SqlsessionUtil. GetSqlCurrentSqlsession ();
//dowork
Invoke=method. Invoke (target, args);
//to commit the transaction
sqlSession.commit();
} the catch (Exception e) {
//rollback
SqlsessionUtil. RollBack (sqlSession);
e.printStackTrace();

} the finally {
SqlsessionUtil. Close (sqlSession);
}

Return the invoke.
}


}


UserDao and UserDaoImpl
 public interface UserDao {

/* *
* save user
* @ param usre
* @ return
*/
Int the save (User usre);
}
Public class UserDaoImpl implements UserDao {


@ Override
Public int the save (User User) {
Return SqlsessionUtil. GetSqlCurrentSqlsession (). Insert (" save ", the user);
}
}


The test class
 
Public void TESTUTILS () {
UserDao UserDao=(UserDao) new TransactionHandler (new UserDaoImpl ()). GetProxy ();
User User=new User ();
User. SetSex (" male ");
User. Elegantly-named setName (" "); [code=Java]


Int I=userDao. Save (user);
System.out.println(i);
} [/code]

Debugging found the second from the threadlocal sqlsession is null
Debugging first getSqlCurrentSqlsession () :
 public static SqlSession getSqlCurrentSqlsession () {
SqlSession SqlSession=threadLocal. The get ();//defaultsqlsession @ 1871
If (sqlSession==null) {
SqlSession=sqlSessionFactory. OpenSession ();
ThreadLocal. Set (sqlSession);
}
Return the sqlSession;
}

For the second time in found out from the threadlocal is null, so again to create a sqlsession last error
 public static SqlSession getSqlCurrentSqlsession () {
SqlSession SqlSession=threadLocal. The get ();//defaultsqlsession @ 1875
If (sqlSession==null) {
SqlSession=sqlSessionFactory. OpenSession ();
ThreadLocal. Set (sqlSession);
}
Return the sqlSession;
}

An error code for:
Org. Apache. Ibatis. Exceptions. PersistenceException:
# # # Error committing the transaction. Cause: org. Apache. Ibatis. Executor. ExecutorException: always commit, a transaction is already closed
# # # Cause: org. Apache. Ibatis. Executor. ExecutorException: always commit, a transaction is already closed
The at org. Apache. Ibatis. Exceptions. ExceptionFactory. WrapException (30) ExceptionFactory. Java:
At MIT (227) DefaultSqlSession. Java: org.apache.ibatis.session.defaults.DefaultSqlSession.com
At MIT (218) DefaultSqlSession. Java: org.apache.ibatis.session.defaults.DefaultSqlSession.com
At com. Li. Utils. TransactionHandler. Invoke (TransactionHandler. Java: 35)
At com. Sun. Proxy. $Proxy6. Save (Unknown Source)
At com. Li. Test. TESTUTILS (58) test. The Java:
At sun. Reflect. NativeMethodAccessorImpl. Invoke0 (Native Method)
At sun. Reflect. NativeMethodAccessorImpl. Invoke (NativeMethodAccessorImpl. Java: 62)
At sun. Reflect. DelegatingMethodAccessorImpl. Invoke (43) DelegatingMethodAccessorImpl. Java:
The at Java. Lang. Reflect. Method. Invoke (498) Method. The Java:
The at org. Junit. Runners. Model. FrameworkMethod $1. RunReflectiveCall (FrameworkMethod. Java: 44)
The at org. Junit. Internal. Runners. Model. ReflectiveCallable. Run (ReflectiveCallable. Java: 15)
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related