@SuppressWarnings("all") private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) { Transaction tx = null; try { final Environment environment = getConfiguration().getEnvironment(); final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment); DataSource ds = DataSourceHolder.currentDataSource().getNative(); if (ds == null) { ds = environment.getDataSource(); } tx = transactionFactory.newTransaction(ds, level, autoCommit); final Executor executor = getConfiguration().newExecutor(tx, execType); return new DefaultSqlSession(getConfiguration(), executor, autoCommit); } catch (Exception e) { closeTransaction(tx); // may have fetched a connection so lets call close() throw ExceptionFactory.wrapException("Error opening session. Cause: " + e, e); } finally { ErrorContext.instance().reset(); } }
@Override public SqlSessionFactory getObject() throws Exception { SqlSessionFactory sqlSessionFactory = super.getObject(); if (sqlSessionFactory instanceof DefaultSqlSessionFactory) return proxy(sqlSessionFactory, methodInvocation -> { if (methodInvocation.getMethod().getName().equals("openSession")) { SqlSession session = (SqlSession) methodInvocation.proceed(); if (session instanceof DefaultSqlSession) { DefaultSqlSession defaultSqlSession = (DefaultSqlSession) session; Executor executor = (Executor) ReflectionUtils.getFieldValue(defaultSqlSession, "executor"); if (executor instanceof CachingExecutor) { CachingExecutor cachingExecutor = (CachingExecutor) executor; SimpleExecutor ex = (SimpleExecutor) ReflectionUtils.getFieldValue(cachingExecutor, "delegate"); System.out.println("ex-> " + ex.getClass().getName()); ReflectionUtils.setDeclaredFieldValue(cachingExecutor, "delegate", proxy(ex, invocation -> { System.out.println("method->" + invocation.getMethod()); return invocation.proceed(); })); } } return session; } return methodInvocation.proceed(); }); return sqlSessionFactory; }
@Test public void SqlSessionをJxSqlSessionに加工して返却する() { Configuration config = new Configuration(); SqlSession session = new DefaultSqlSession(config, null); SqlSession wrapped = factory.wrap(session); assertThat(wrapped, is(instanceOf(JxSqlSession.class))); assertThat(wrapped.getConfiguration(), is(config)); // delegate 確認 }
public SqlSession session(Connection conn) { Transaction transaction = new ManagedTransaction(conn, false); SimpleExecutor excutor = new SimpleExecutor(configuration, transaction); return new DefaultSqlSession(configuration, excutor); }
@Override protected SqlSession getSqlSession() { return new DefaultSqlSession(this.configuration, this.executor); }
@Override protected SqlSession getSqlSession() { return new DefaultSqlSession(this.configuration, this.executor, false); }