day47-JDBC和连接池03( 三 )


day47-JDBC和连接池03

文章插图
day47-JDBC和连接池03

文章插图
9.2批处理源码分析在上述代码中 , 在preparedStatement.addBatch();语句旁打上断点 , 点击debug , 点击step into
【day47-JDBC和连接池03】
day47-JDBC和连接池03

文章插图
可以看到光标跳转到了如下方法:
public void addBatch() throws SQLException {if (this.batchedArgs == null) {this.batchedArgs = new ArrayList();}this.batchedArgs.add(new PreparedStatement.BatchParams(this.parameterValues, this.parameterStreams, this.isStream, this.streamLengths, this.isNull));}第一次执行该方法时 , 会创建Arraylist类型的对象集合elementDate=>Object[] , elementDate=>Object[]用来存放我们预处理的SQL语句 。当elementDate满后 , 就按照1.5倍扩容
当添加到指定的值后 , 就会执行executeBatch();
批处理会减少我们发送SQL语句的网络开销 , 并且减少编译次数 , 因此效率提高了
1.5倍扩容:
day47-JDBC和连接池03

文章插图
day47-JDBC和连接池03

文章插图
9.3.事务和批处理的区别
  • 事务:事务底层是在数据库方存储SQL , 没有提交事务的数据放在数据库的临时表空间 。最后一次提交是把临时表空间的数据提交到数据库服务器执行事务消耗的是数据库服务器内存
  • 批处理:
    批处理底层是在客户端存储SQL最后一次执行批处理是把客户端存储的数据发送到数据库服务器执行 。批处理消耗的是客户端的内存

经验总结扩展阅读