- 浏览: 954047 次
文章分类
- 全部博客 (428)
- Hadoop (2)
- HBase (1)
- ELK (1)
- ActiveMQ (13)
- Kafka (5)
- Redis (14)
- Dubbo (1)
- Memcached (5)
- Netty (56)
- Mina (34)
- NIO (51)
- JUC (53)
- Spring (13)
- Mybatis (17)
- MySQL (21)
- JDBC (12)
- C3P0 (5)
- Tomcat (13)
- SLF4J-log4j (9)
- P6Spy (4)
- Quartz (12)
- Zabbix (7)
- JAVA (9)
- Linux (15)
- HTML (9)
- Lucene (0)
- JS (2)
- WebService (1)
- Maven (4)
- Oracle&MSSQL (14)
- iText (11)
- Development Tools (8)
- UTILS (4)
- LIFE (8)
最新评论
-
Donald_Draper:
Donald_Draper 写道刘落落cici 写道能给我发一 ...
DatagramChannelImpl 解析三(多播) -
Donald_Draper:
刘落落cici 写道能给我发一份这个类的源码吗Datagram ...
DatagramChannelImpl 解析三(多播) -
lyfyouyun:
请问楼主,执行消息发送的时候,报错:Transport sch ...
ActiveMQ连接工厂、连接详解 -
ezlhq:
关于 PollArrayWrapper 状态含义猜测:参考 S ...
WindowsSelectorImpl解析一(FdMap,PollArrayWrapper) -
flyfeifei66:
打算使用xmemcache作为memcache的客户端,由于x ...
Memcached分布式客户端(Xmemcached)
JDBC驱动初始化-Mysql:http://donald-draper.iteye.com/blog/2342010
JDBC连接的获取:http://donald-draper.iteye.com/blog/2342011
Mysql负载均衡连接的获取:http://donald-draper.iteye.com/blog/2342089
Mysql主从复制读写分离连接的获取:http://donald-draper.iteye.com/blog/2342108
ConnectionImp创建MysqlIO :http://donald-draper.iteye.com/blog/2342959
Mysql预编译SQL:http://donald-draper.iteye.com/blog/2342960
MysqlSQL PreparedStatement的查询:http://donald-draper.iteye.com/blog/2343083
MySQL ServerPreparedStatement查询:http://donald-draper.iteye.com/blog/2343124
再前面说过PreparedStatement的查询,今天看一下ServerPreparedStatement的查询,
这篇文章要结合PreparedStatement的查询来理解。
//ServerPreparedStatement执行查询的时候,调用的是PreparedStatement的查询方法
//ServerPreparedStatement
executeInternal此方法在PreparedStatement有实现,由于ServerPreparedStatement继承
PreparedStatement,重写了这个方法,我们来看ServerPreparedStatement的这个方法
//ServerPreparedStatement查询
总结:
ServerPreparedStatement的查询与PreparedStatement的思想基本上一直的都是首先组装查询包,并通MysqlIO将包发送到Server,Server返回查询结果,MysqlIO将返回结果转换为ResultSetInternalMethods(ResultSetImpl),即ResultSet。
JDBC连接的获取:http://donald-draper.iteye.com/blog/2342011
Mysql负载均衡连接的获取:http://donald-draper.iteye.com/blog/2342089
Mysql主从复制读写分离连接的获取:http://donald-draper.iteye.com/blog/2342108
ConnectionImp创建MysqlIO :http://donald-draper.iteye.com/blog/2342959
Mysql预编译SQL:http://donald-draper.iteye.com/blog/2342960
MysqlSQL PreparedStatement的查询:http://donald-draper.iteye.com/blog/2343083
MySQL ServerPreparedStatement查询:http://donald-draper.iteye.com/blog/2343124
再前面说过PreparedStatement的查询,今天看一下ServerPreparedStatement的查询,
这篇文章要结合PreparedStatement的查询来理解。
//ServerPreparedStatement执行查询的时候,调用的是PreparedStatement的查询方法
//ServerPreparedStatement
public ResultSet executeQuery() throws SQLException { checkClosed(); ConnectionImpl locallyScopedConn = connection; checkForDml(originalSql, firstCharOfStmt); CachedResultSetMetaData cachedMetadata = null; synchronized(locallyScopedConn.getMutex()) { if(locallyScopedConn.useMaxRows()) { if(hasLimitClause) { //没有分页语句的查询 results = executeInternal(maxRows, sendPacket, createStreamingResultSet(), true, metadataFromCache, false); } else { if(maxRows <= 0) executeSimpleNonQuery(locallyScopedConn, "SET OPTION SQL_SELECT_LIMIT=DEFAULT"); else executeSimpleNonQuery(locallyScopedConn, "SET OPTION SQL_SELECT_LIMIT=" + maxRows); results = executeInternal(-1, sendPacket, doStreaming, true, metadataFromCache, false); if(oldCatalog != null) connection.setCatalog(oldCatalog); } } else { results = executeInternal(-1, sendPacket, doStreaming, true, metadataFromCache, false); } } return results; }
executeInternal此方法在PreparedStatement有实现,由于ServerPreparedStatement继承
PreparedStatement,重写了这个方法,我们来看ServerPreparedStatement的这个方法
public class ServerPreparedStatement extends com.mysql.jdbc.PreparedStatement { //执行查询,返回查询结果集 protected ResultSetInternalMethods executeInternal(int maxRowsToRetrieve, Buffer sendPacket, boolean createStreamingResultSet, boolean queryIsSelectOnly, Field metadataFromCache[], boolean isBatch) throws SQLException { numberOfExecutions++; //委托给serverExecute方法 return serverExecute(maxRowsToRetrieve, createStreamingResultSet, metadataFromCache); } }
//ServerPreparedStatement查询
private ResultSetInternalMethods serverExecute(int maxRowsToRetrieve, boolean createStreamingResultSet, Field metadataFromCache[]) throws SQLException { //获取互斥锁 Object obj = connection.getMutex(); JVM INSTR monitorenter ; MysqlIO mysql; Buffer packet; long begin; boolean logSlowQueries; boolean gatherPerformanceMetrics; StatementImpl.CancelTask timeoutTask; //从连接获取MysqlIO mysql = connection.getIO(); //从MysqlIO获取查询包 packet = mysql.getSharedSendPacket(); packet.clear(); packet.writeByte((byte)23); packet.writeLong(serverStatementId); boolean usingCursor = false; if(connection.versionMeetsMinimum(4, 1, 2)) { if(resultFields != null && connection.isCursorFetchEnabled() && getResultSetType() == 1003 && getResultSetConcurrency() == 1007 && getFetchSize() > 0) { packet.writeByte((byte)1); usingCursor = true; } else { packet.writeByte((byte)0); } packet.writeLong(1L); } int nullCount = (parameterCount + 7) / 8; int nullBitsPosition = packet.getPosition(); for(int i = 0; i < nullCount; i++) packet.writeByte((byte)0); byte nullBitsBuffer[] = new byte[nullCount]; packet.writeByte(((byte)(sendTypesToServer ? 1 : 0))); if(sendTypesToServer) { for(int i = 0; i < parameterCount; i++) packet.writeInt(parameterBindings[i].bufferType); } for(int i = 0; i < parameterCount; i++) { if(parameterBindings[i].isLongData) continue; if(!parameterBindings[i].isNull) storeBinding(packet, parameterBindings[i], mysql); else nullBitsBuffer[i / 8] |= 1 << (i & 7); } int endPosition = packet.getPosition(); packet.setPosition(nullBitsPosition); packet.writeBytesNoNull(nullBitsBuffer); packet.setPosition(endPosition); begin = 0L; logSlowQueries = connection.getLogSlowQueries(); gatherPerformanceMetrics = connection.getGatherPerformanceMetrics(); if(profileSQL || logSlowQueries || gatherPerformanceMetrics) begin = mysql.getCurrentTimeNanosOrMillis(); resetCancelledState(); timeoutTask = null; ResultSetInternalMethods resultsetinternalmethods; if(connection.getEnableQueryTimeouts() && timeoutInMillis != 0 && connection.versionMeetsMinimum(5, 0, 0)) { //如果启动延时查询,创建延时查询TimeTask,并通过ConnectionImpl的CancelTimer(Timer)去调度 timeoutTask = new StatementImpl.CancelTask(this, this); connection; ConnectionImpl.getCancelTimer().schedule(timeoutTask, timeoutInMillis); } //MysqlIO向Server发送查询命令 Buffer resultPacket = mysql.sendCommand(23, null, packet, false, null, 0); long queryEndTime = 0L; if(logSlowQueries || gatherPerformanceMetrics || profileSQL) queryEndTime = mysql.getCurrentTimeNanosOrMillis(); if(timeoutTask != null) { timeoutTask.cancel(); if(timeoutTask.caughtWhileCancelling != null) throw timeoutTask.caughtWhileCancelling; timeoutTask = null; } synchronized(cancelTimeoutMutex) { if(wasCancelled) { SQLException cause = null; if(wasCancelledByTimeout) cause = new MySQLTimeoutException(); else cause = new MySQLStatementCancelledException(); resetCancelledState(); throw cause; } } //转化结果集,这个我们在PreparedStatement,看过,这里就不说了 ResultSetInternalMethods rs = mysql.readAllResults(this, maxRowsToRetrieve, resultSetType, resultSetConcurrency, createStreamingResultSet, currentCatalog, resultPacket, true, fieldCount, metadataFromCache); return resultsetinternalmethods; }
总结:
ServerPreparedStatement的查询与PreparedStatement的思想基本上一直的都是首先组装查询包,并通MysqlIO将包发送到Server,Server返回查询结果,MysqlIO将返回结果转换为ResultSetInternalMethods(ResultSetImpl),即ResultSet。
发表评论
-
Mysql PreparedStatement 批处理
2016-12-06 18:09 1323JDBC驱动初始化-Mysql:http://donald-d ... -
MysqlSQL PreparedStatement的查询
2016-12-06 11:40 1975JDBC驱动初始化-Mysql:http://donald-d ... -
Mysql预编译SQL
2016-12-05 19:06 1110JDBC驱动初始化-Mysql:http://donald-d ... -
ConnectionImp创建MysqlIO
2016-12-05 19:01 963JDBC驱动初始化-Mysql:http://donald-d ... -
Mysql主从复制读写分离连接的获取
2016-12-01 08:43 1111JDBC驱动初始化-Mysql:http://donald-d ... -
Mysql负载均衡连接的获取
2016-12-01 08:38 1264Java动态代理:http://www.cnblogs.com ... -
JDBC连接的获取
2016-11-30 12:44 2874JDBC驱动初始化-Mysql:http://donald-d ... -
JDBC驱动初始化-Mysql
2016-11-30 12:41 3002JDBC驱动初始化-Mysql:http://donald-d ... -
mysql 存储过程
2016-07-17 14:49 862存储过程基础:http://sishuok.com/forum ... -
java.sql.Date,java.util.Date,java.sql.Timestamp的区别
2016-07-17 11:50 841java.sql.Date,jdbc映射数据库中的date类型 ... -
JDBC PreparedStatement 的用法
2016-07-15 17:27 851import java.sql.Connection; im ...
相关推荐
Mysql 时间模糊查询,不同场景的查询方式,根据条件查询
第3课 查询优化技术理论与MySQL实践(一)------子查询的优化(一) 第4课 查询优化技术理论与MySQL实践(二)------子查询的优化(二) 从理论看,子查询包括的内容和范围,建立清晰的概念 从实践看,MySQL的子查询...
sql查询优化 提高MySQL数据库查询效率的几个技巧
mysql 树形结构查询,使用存储过程,实现mysql的树形结构查询
MySQL递归查询,oracle在迁移成mysql后难免会遇到的问题。
关系型数据库+Mysql+查询用户连续登陆天数+数据统计 关系型数据库+Mysql+查询用户连续登陆天数+数据统计 关系型数据库+Mysql+查询用户连续登陆天数+数据统计 关系型数据库+Mysql+查询用户连续登陆天数+数据统计 关系...
两种mysql递归tree查询效率-mysql递归tree,提供两种递归算法
mysql递归查询,适合多分类查询。对于数据库中的树形结构数据,如部门表,有时候,我们需要知道某部门的所有下属部分或者某部分的所有上级部门,这时候就需要用到mysql的递归查询
mysql可以查询根据某列按条件统计总数
资源名字:基于php+mysql的成绩查询系统设计与实现(源码+文档)_php_mysql_成绩查询系统.zip 资源内容:项目全套源码+完整文档 源码说明: 全部项目源码都是经过测试校正后百分百成功运行。 适合场景:相关项目设计...
mysql中有种可以通过join相关操作进行表与表之间的方式查询不同结果集,但是在一对多的情况下,关键查询的结果是多条的.例如:班级和学习的关系,我想很直观的看到班级和学生的情况,列表显示出班级的信息和班级的男生...
mysql 查询本周、上周、本月、上个月份的数据 mysql 查询本周、上周、本月、上个月份的数据 mysql 查询本周、上周、本月、上个月份的数据
mysql递归查询.txt,里面写了mysql递归查询的例子,方便需要的朋友使用,可读性强,简单实用
mysql api查询手册 chm版本 清晰版 mysql api查询手册 chm版本 清晰版 不得用于商业
基于php和mysql的成绩查询系统,pdf格式的文件,大家可以参考一下。
MySQL单表查询练习题
MySQL快速查询命令mysqlshow使用
mysql 慢查询页面工具 页面化分析慢日志
c++实现mysql分页查询
page ssm mysql 分页查询 绝对的强大