connection创建Statement对象,默认是使用resultSet的读取类型
Statement在编译的时候不进行sql语句的检测,只是把Sql语句看成一个String,只有在驱动程序执行sql语句的时候才去检查sql语句的正确性
一个Statement对应一个操作的过程,如果想对多个结果集操作就要创建多个Statement
当你使用一个Statement操作多个结果集的时候,当你在操作第二个结果集的时候就隐含着对第一个结果集的关闭
public java.sql.Statement createStatement()
throws SQLException
{
return createStatement(1003, 1007);
}
public java.sql.Statement createStatement(int resultSetType, int resultSetConcurrency)
throws SQLException
{
checkClosed();
StatementImpl stmt = new StatementImpl(getLoadBalanceSafeProxy(), this.database);
stmt.setResultSetType(resultSetType);
stmt.setResultSetConcurrency(resultSetConcurrency);
return stmt;
}
在使用Statement操作表的过程,执行executeQuery(sql)执行的过程会有一个锁来锁住Connection对象的锁
这样对于同一个connection同一时刻只能执行一个操作
5.1.15操作查询数据库的代码
public ResultSet executeQuery(String sql)
throws SQLException
{
checkClosed();
MySQLConnection locallyScopedConn = this.connection;
synchronized (locallyScopedConn.getMutex()) {
this.retrieveGeneratedKeys = false;
......
}
//5.1.20操作数据库的代码
//而5.1.20是直接锁主这个connection对象 同时会锁住这个方法
但是5.1.15是锁住connection对象的锁没有锁住这个对象的方法
一个是方法锁 一个代码块锁
public synchronized ResultSet executeQuery(String sql)
throws SQLException
{
checkClosed();
MySQLConnection locallyScopedConn = this.connection;
synchronized (locallyScopedConn) {
this.retrieveGeneratedKeys = false
...}
个人理解在性能方面影响不会很大 但是在多线程环境中肯定还是有影响,因为如果在方法加锁表示进入这个方法的线程会先锁住这个方法,其它线程只能等待,而代码块表示只有进入代码块的线程才会锁住,而可以同时多个线程进入这个方法,进入代码块的时候会处于等待。