Connection connection = sqlSession.getConnection();
public class DefaultSqlSession implements SqlSession {
private final Configuration configuration;
private final Executor executor;
@Override
public Connection getConnection() {
return executor.getTransaction().getConnection();
}
}
public class CachingExecutor implements Executor {
private final Executor delegate;
public CachingExecutor(Executor delegate) {
this.delegate = delegate;
delegate.setExecutorWrapper(this);
}
@Override
public Transaction getTransaction() {
return delegate.getTransaction();
}
}
public class ManagedTransaction implements Transaction {
private DataSource dataSource;
private TransactionIsolationLevel level;
private Connection connection;
@Override
public Connection getConnection() throws SQLException {
if (this.connection == null) {
openConnection();
}
return this.connection;
}
protected void openConnection() throws SQLException {
this.connection = this.dataSource.getConnection(); // 111
if (this.level != null) {//设置事务隔离级别
this.connection.setTransactionIsolation(this.level.getLevel());//222
}
}
}
//111
public class MysqlDataSource extends JdbcPropertySetImpl implements DataSource, Referenceable, Serializable, JdbcPropertySet {
@Override
public java.sql.Connection getConnection() throws SQLException {
return getConnection(this.user, this.password);
}
@Override
public java.sql.Connection getConnection(String userID, String pass) throws SQLException {
Properties props = exposeAsProperties();//获取数据库用户名和密码
if (userID != null) {
props.setProperty(PropertyKey.USER.getKeyName(), userID);
}
if (pass != null) {
props.setProperty(PropertyKey.PASSWORD.getKeyName(), pass);
}
return getConnection(props);
}
protected java.sql.Connection getConnection(Properties props) throws SQLException {
String jdbcUrlToUse = this.explicitUrl ? this.url : getUrl();//获取数据库url
return mysqlDriver.connect(jdbcUrlToUse, props);
}
}
public class NonRegisteringDriver implements java.sql.Driver {
@Override
public java.sql.Connection connect(String url, Properties info) throws SQLException {
if (!ConnectionUrl.acceptsUrl(url)) {
return null;
}
ConnectionUrl conStr = ConnectionUrl.getConnectionUrlInstance(url, info);
switch (conStr.getType()) {
case SINGLE_CONNECTION:
return com.mysql.cj.jdbc.ConnectionImpl.getInstance(conStr.getMainHost()); // 666
case FAILOVER_CONNECTION:
case FAILOVER_DNS_SRV_CONNECTION:
return FailoverConnectionProxy.createProxyInstance(conStr); //777
case LOADBALANCE_CONNECTION:
case LOADBALANCE_DNS_SRV_CONNECTION:
return LoadBalancedConnectionProxy.createProxyInstance(conStr);
case REPLICATION_CONNECTION:
case REPLICATION_DNS_SRV_CONNECTION:
return ReplicationConnectionProxy.createProxyInstance(conStr);
default:
return null;
}
}
}
//666: 根据主机信息获取连接 : ConnectionImpl impl JdbcConnection ext java.sql.Connection
public class ConnectionImpl implements JdbcConnection, SessionEventListener, Serializable {
public static JdbcConnection getInstance(HostInfo hostInfo) throws SQLException {
return new ConnectionImpl(hostInfo);//构造方法中初始化主机信息
}
}
//777
public class FailoverConnectionProxy extends MultiHostConnectionProxy {
public static JdbcConnection createProxyInstance(ConnectionUrl connectionUrl) throws SQLException {
FailoverConnectionProxy connProxy = new FailoverConnectionProxy(connectionUrl);
//创建代理
return (JdbcConnection) java.lang.reflect.Proxy.newProxyInstance(JdbcConnection.class.getClassLoader(), new Class<?>[] { JdbcConnection.class },
connProxy);
}
}
package com.mysql.cj.jdbc;//Mysql实现
public class ConnectionImpl implements JdbcConnection, SessionEventListener, Serializable {//222
//默认隔离级别 READ_COMMITTED
private int isolationLevel = java.sql.Connection.TRANSACTION_READ_COMMITTED;
//更新隔离级别
private NativeSession session = null;
@Override
public void setTransactionIsolation(int level) throws SQLException {
synchronized (getConnectionMutex()) {
checkClosed();
String sql = null;
boolean shouldSendSet = false;
if (this.propertySet.getBooleanProperty(PropertyKey.alwaysSendSetIsolation).getValue()) {
shouldSendSet = true;
} else {
if (level != this.isolationLevel) {//隔离级别与默认不相等
shouldSendSet = true;
}
}
if (this.useLocalSessionState.getValue()) {
shouldSendSet = this.isolationLevel != level;
}
if (shouldSendSet) {
switch (level) {
case java.sql.Connection.TRANSACTION_NONE: //0
throw SQLError.createSQLException(Messages.getString("Connection.24"), getExceptionInterceptor());
case java.sql.Connection.TRANSACTION_READ_COMMITTED: //1
sql = "SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED";
break;
case java.sql.Connection.TRANSACTION_READ_UNCOMMITTED://2
sql = "SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED";
break;
case java.sql.Connection.TRANSACTION_REPEATABLE_READ: //4
sql = "SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ";
break;
case java.sql.Connection.TRANSACTION_SERIALIZABLE: //8
sql = "SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE";
break;
default:
throw SQLError.createSQLException(Messages.getString("Connection.25", new Object[] { level }),
MysqlErrorNumbers.SQL_STATE_DRIVER_NOT_CAPABLE, getExceptionInterceptor());
}
//调用 com.mysql.cj.NativeSession 执行更新
this.session.execSQL(null, sql, -1, null, false, this.nullStatementResultSetFactory, null, false);//执行sql,更新隔离级别
this.isolationLevel = level;//成员变量隔离级别重新赋值
}
}
}
}
public interface Connection extends Wrapper, AutoCloseable {
int TRANSACTION_NONE = 0;
int TRANSACTION_READ_UNCOMMITTED = 1;
int TRANSACTION_READ_COMMITTED = 2;
int TRANSACTION_REPEATABLE_READ = 4;
int TRANSACTION_SERIALIZABLE = 8;
}
mybatis sqlSession.getConnection()调用过程
最新推荐文章于 2024-03-07 14:29:04 发布