JDBC 线程安全 数据库连接池
jdbc 是线程安全的,但是,推荐一个线程用一个链接
JDBC is thread safe: It is quite OK to pass the various JDBC objects between threads.
For example, you can create the connection in one thread; another thread can use this connection to create a PreparedStatement and a third thread can process the result set. The single major restriction is that you cannot have more than one ResultSet open on a single PreparedStatement at any time. See Does Oracle DB support multiple (parallel) operations per connection?
你不能在一个statment上面存在超过一个打开的resultset(不打开的可以有多个)。
Note that a database commit occurs on a Connection, and so all DML (INSERT, UPDATE and DELETE's) on that connection will commit together. Therefore, if you want to support multiple transactions at the same time, you must have at least one Connection for each concurrent Transaction.
至少一个事物对应一个链接
Users often ask me if our JDBC driver supports multithreaded programming. The answer I always give is a qualifed 'yes'....'but you shouldn't be doing it!'.
mysql的jdbc驱动是线程安全的,但是我们不应该这样用。
Although the JDBC API requires that JDBC drivers support multithreaded access, the JDBC API itself is not designed to be used in a multithreaded way. It is only intended that multithreaded access will not cause the driver to enter an 'unknown' state with regards to communications to the database.
jdbc 要求驱动支持多线程,但他设计不是为了多线程使用。
多线程公用一个connection会引发的问题
1、Committing or rolling back a transaction closes all open ResultSet objects and currently executing Statements, unless you are using held cursors.
If one thread commits, it closes the Statements and ResultSets of all other threads using the same connection.
如果一个线程提交或回滚一个事物会关闭所有打开的 resultset、statement
2、Executing a Statement automatically closes any existing open ResultSet generated by an earlier execution of that Statement.
If threads share Statements, one thread could close another's ResultSet.
执行一个Statement会关闭已经存在的ResultSet
如果多线程共享 Statements,别的线程可能会关闭其他线程的 resultset
数据库连接池的实现及原理
JDBC是一个规范,遵循JDBC接口规范,各个数据库厂家各自实现自己的驱动程序(Driver),如下图所示:

JDBC最常用的资源有三类:
— Connection: 数据库连接。
— Statement: 会话声明。
— ResultSet: 结果集游标。

如果想确定某个数据库连接(Connection)是否超时,则需要确定其(所有的)子Statement是否超时,同样,需要确定所有相关的 ResultSet是否超时;
在关闭Connection前,需要关闭所有相关的Statement和ResultSet。
有些数据库的JDBC Driver并不支持Connection与Statement之间的逻辑连接功能,如SQLServer,我们只能等待她自身的更新版本了。
http://download.nust.na/pub6/mysql/news-and-events/newsletter/2003-04/a0000000154.html
JDBC 线程安全 数据库连接池的更多相关文章
- JDBC整合c3p0数据库连接池 解决Too many connections错误
前段时间,接手一个项目使用的是原始的jdbc作为数据库的访问,发布到服务器上在运行了一段时间之后总是会出现无法访问的情况,登录到服务器,查看tomcat日志发现总是报如下的错误. Caused by: ...
- 【JDBC核心】数据库连接池
数据库连接池 传统模式 使用数据库的传统模式: 在主程序(servlet.beans等)中建立数据库连接: 进行 SQL 操作: 断开数据库连接. 这种模式存在的问题: JDBC 连接数据库的方式(四 ...
- JAVA企业级开发-jdbc事务,数据库连接池(10)
一. JDBC事务 事务: 问题1.什么是事务 问题2.java中(jdbc)如何控制事务 1. 事务—重点 指的的逻辑上的一组(一组sql,insert update ,delete)操作,组成 ...
- spring配置tomcat jdbc pool数据库连接池
<bean id="sqliteDataSource" class="org.apache.tomcat.jdbc.pool.DataSource" de ...
- j2ee数据库连接池配置大全
<!--web.xml begin--> <!--web.xml Spring ApplicationContext配置文件的路径 ,可使用通配符,多个路径用,号分隔 --> ...
- Java数据库连接池原理与简易实现
1.什么是数据库连接池 我们现在在开发中一定都会用到数据库,为了提高我们的系统的访问速度,数据库优化是一个有效的途径.我们现在开发中使用数据库一般都要经历以下的四个步骤:(1)加载数据库的驱动类,(2 ...
- 数据库连接池在Tomcat中的几种配置方法
数据库连接是一种关键的有限的昂贵的资源,这在多用户网页应用程序中体现的尤为突出.对数据库连接的管理能显著影响到整个应用程序的伸缩性和健壮性,影响到程序的性能指标,数据库连接池正是针对这个问题提出的. ...
- Spring Boot 数据库连接池参数
挑战A.I.,赢百万奖金......了解更多详情>>> Tomcat JDBC 连接池 Spring Boot 默认选择 Tomcat JDBC Pool 作为数据库连接池.Tomc ...
- JAVA和C#中数据库连接池原理与应用
JAVA和C#中数据库连接池原理 在现在的互联网发展中,高并发成为了主流,而最关键的部分就是对数据库操作和访问,在现在的互联网发展中,ORM框架曾出不穷, 比如:.Net-Core的EFCore.Sq ...
随机推荐
- java InputStream的使用
package cn.kongxh.io3;import java.io.File ;import java.io.InputStream ;import java.io.FileInputStrea ...
- python笔记43-加解密AES/CBC/pkcs7padding
前言 有些公司对接口的安全要求比较高,传参数的时候,不会明文的传输,先对接口加密,返回的数据也加密返回. 目前比较常见的加密方式是AES/CBC/pkcs7padding. AES五种加密模式 在AE ...
- ROS学习(更新中~)
1.一次把ROS环境变量都自动配置好(即添加到bash会话中)echo "source /opt/ros/indigo/setup.bash" >> ~/.bashrc ...
- 牛客NOIP暑期七天营-普及组2D
链接:https://ac.nowcoder.com/acm/contest/926/D来源:牛客网 在一维坐标系中,给定 n条有颜色的线段,第 i条线段的左右端点分别为 li和 ri,此外它的颜 ...
- Centos7.x for aarch64 下载地址
ARM64架构系统 CentOS7镜像下载源 http://archive.kernel.org/centos-vault/altarch/ 例如:centos7.5 http://archive.k ...
- iframe跨域修改子页面内容或样式
哈哈 当然不能啦 要是能修改百度logo早就是我的头像了
- SpringBoot简便地打成一个war包
正常情况下SpringBoot项目是以jar包的形式,通过命令行: java -jar demo.jar 来运行的,并且SpringBoot是内嵌Tomcat服务器,所以每次重新启动都是用的新的Tom ...
- ios app ipv6 审核总结
AAAA记录(AAAA record)是用来将域名解析到IPv6地址的DNS记录.用户可以将一个域名解析到IPv6地址上,也可以将子域名解析到IPv6地址上. 1)$ dig +nocmd +nost ...
- MongoDB shell 3 集合方法
方法名 描述 db.collection.aggregate() 聚合,主要用于处理数据(诸如统计平均值,求和等),并返回计算后的数据结果 db.collection.bulkWrite() 批量写入 ...
- BZOJ 1802: [Ahoi2009]checker
题目描述 若有两个红格相邻 第一问的答案为0,所有位置上的棋子都可以通过在这两个格子上放棋子得到 第二设f[i]表示想让第i个格子上有棋子需要放的棋子数 若没有,第一问答案为偶数格子上白格的个数,第二 ...