SolonSqlSession.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package com.baomidou.mybatisplus.solon.integration;
  2. import org.apache.ibatis.cursor.Cursor;
  3. import org.apache.ibatis.executor.BatchResult;
  4. import org.apache.ibatis.reflection.ExceptionUtil;
  5. import org.apache.ibatis.session.Configuration;
  6. import org.apache.ibatis.session.ResultHandler;
  7. import org.apache.ibatis.session.RowBounds;
  8. import org.apache.ibatis.session.SqlSession;
  9. import org.apache.ibatis.session.SqlSessionFactory;
  10. import org.noear.solon.data.tran.TranUtils;
  11. import java.lang.reflect.InvocationHandler;
  12. import java.lang.reflect.Method;
  13. import java.lang.reflect.Proxy;
  14. import java.sql.Connection;
  15. import java.util.List;
  16. import java.util.Map;
  17. public class SolonSqlSession implements SqlSession {
  18. private final SqlSessionFactory sqlSessionFactory;
  19. private final SqlSession sqlSessionProxy;
  20. public SolonSqlSession(SqlSessionFactory sqlSessionFactory) {
  21. this.sqlSessionFactory = sqlSessionFactory;
  22. this.sqlSessionProxy = (SqlSession) Proxy.newProxyInstance(SqlSessionFactory.class.getClassLoader(), new Class[]{SqlSession.class}, new SqlSessionInterceptor());
  23. }
  24. public SqlSessionFactory getSqlSessionFactory() {
  25. return this.sqlSessionFactory;
  26. }
  27. public SqlSession getSqlSessionProxy() {
  28. return this.sqlSessionProxy;
  29. }
  30. @Override
  31. public <T> T selectOne(String statement) {
  32. return this.sqlSessionProxy.selectOne(statement);
  33. }
  34. @Override
  35. public <T> T selectOne(String statement, Object parameter) {
  36. return this.sqlSessionProxy.selectOne(statement, parameter);
  37. }
  38. @Override
  39. public <K, V> Map<K, V> selectMap(String statement, String mapKey) {
  40. return this.sqlSessionProxy.selectMap(statement, mapKey);
  41. }
  42. @Override
  43. public <K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey) {
  44. return this.sqlSessionProxy.selectMap(statement, parameter, mapKey);
  45. }
  46. @Override
  47. public <K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds) {
  48. return this.sqlSessionProxy.selectMap(statement, parameter, mapKey, rowBounds);
  49. }
  50. @Override
  51. public <T> Cursor<T> selectCursor(String statement) {
  52. return this.sqlSessionProxy.selectCursor(statement);
  53. }
  54. @Override
  55. public <T> Cursor<T> selectCursor(String statement, Object parameter) {
  56. return this.sqlSessionProxy.selectCursor(statement, parameter);
  57. }
  58. @Override
  59. public <T> Cursor<T> selectCursor(String statement, Object parameter, RowBounds rowBounds) {
  60. return this.sqlSessionProxy.selectCursor(statement, parameter, rowBounds);
  61. }
  62. @Override
  63. public <E> List<E> selectList(String statement) {
  64. return this.sqlSessionProxy.selectList(statement);
  65. }
  66. @Override
  67. public <E> List<E> selectList(String statement, Object parameter) {
  68. return this.sqlSessionProxy.selectList(statement, parameter);
  69. }
  70. @Override
  71. public <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds) {
  72. return this.sqlSessionProxy.selectList(statement, parameter, rowBounds);
  73. }
  74. @Override
  75. public void select(String statement, ResultHandler handler) {
  76. this.sqlSessionProxy.select(statement, handler);
  77. }
  78. @Override
  79. public void select(String statement, Object parameter, ResultHandler handler) {
  80. this.sqlSessionProxy.select(statement, parameter, handler);
  81. }
  82. @Override
  83. public void select(String statement, Object parameter, RowBounds rowBounds, ResultHandler handler) {
  84. this.sqlSessionProxy.select(statement, parameter, rowBounds, handler);
  85. }
  86. @Override
  87. public int insert(String statement) {
  88. return this.sqlSessionProxy.insert(statement);
  89. }
  90. @Override
  91. public int insert(String statement, Object parameter) {
  92. return this.sqlSessionProxy.insert(statement, parameter);
  93. }
  94. @Override
  95. public int update(String statement) {
  96. return this.sqlSessionProxy.update(statement);
  97. }
  98. @Override
  99. public int update(String statement, Object parameter) {
  100. return this.sqlSessionProxy.update(statement, parameter);
  101. }
  102. @Override
  103. public int delete(String statement) {
  104. return this.sqlSessionProxy.delete(statement);
  105. }
  106. @Override
  107. public int delete(String statement, Object parameter) {
  108. return this.sqlSessionProxy.delete(statement, parameter);
  109. }
  110. @Override
  111. public <T> T getMapper(Class<T> type) {
  112. return this.getConfiguration().getMapper(type, this);
  113. }
  114. @Override
  115. public void commit() {
  116. throw new UnsupportedOperationException("Manual commit is not allowed over a Solon managed SqlSession");
  117. }
  118. @Override
  119. public void commit(boolean force) {
  120. throw new UnsupportedOperationException("Manual commit is not allowed over a Solon managed SqlSession");
  121. }
  122. @Override
  123. public void rollback() {
  124. throw new UnsupportedOperationException("Manual rollback is not allowed over a Solon managed SqlSession");
  125. }
  126. @Override
  127. public void rollback(boolean force) {
  128. throw new UnsupportedOperationException("Manual rollback is not allowed over a Solon managed SqlSession");
  129. }
  130. @Override
  131. public void close() {
  132. throw new UnsupportedOperationException("Manual close is not allowed over a Solon managed SqlSession");
  133. }
  134. @Override
  135. public void clearCache() {
  136. this.sqlSessionProxy.clearCache();
  137. }
  138. @Override
  139. public Configuration getConfiguration() {
  140. return this.sqlSessionFactory.getConfiguration();
  141. }
  142. @Override
  143. public Connection getConnection() {
  144. return this.sqlSessionProxy.getConnection();
  145. }
  146. @Override
  147. public List<BatchResult> flushStatements() {
  148. return this.sqlSessionProxy.flushStatements();
  149. }
  150. private class SqlSessionInterceptor implements InvocationHandler {
  151. private SqlSessionInterceptor() {
  152. }
  153. @Override
  154. public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  155. Object unwrapped;
  156. try (SqlSession sqlSession = sqlSessionFactory.openSession(true)) {
  157. Object result = method.invoke(sqlSession, args);
  158. sqlSession.commit(!TranUtils.inTrans());
  159. unwrapped = result;
  160. } catch (Throwable var11) {
  161. unwrapped = ExceptionUtil.unwrapThrowable(var11);
  162. if (unwrapped instanceof RuntimeException) {
  163. throw (RuntimeException) unwrapped;
  164. }
  165. throw (Throwable) unwrapped;
  166. }
  167. return unwrapped;
  168. }
  169. }
  170. }