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,我们只能等待她自身的更新版本了。

参考
https://stackoverflow.com/questions/12192592/java-sql-sqlexception-ora-01000-maximum-open-cursors-exceeded

http://download.nust.na/pub6/mysql/news-and-events/newsletter/2003-04/a0000000154.html

原文地址:https://www.cnblogs.com/siqi/p/11334436.html

时间: 2024-08-29 20:55:27

JDBC 线程安全 数据库连接池的相关文章

JDBC整合c3p0数据库连接池 解决Too many connections错误

前段时间,接手一个项目使用的是原始的jdbc作为数据库的访问,发布到服务器上在运行了一段时间之后总是会出现无法访问的情况,登录到服务器,查看tomcat日志发现总是报如下的错误. Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too man

spring配置tomcat jdbc pool数据库连接池

<bean id="sqliteDataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close"> <!-- <bean class="org.apache.tomcat.jdbc.pool.PoolProperties"> --> <!-- 数据库连接池配置 --> <!--

j2ee数据库连接池配置大全

<!--web.xml begin--> <!--web.xml Spring ApplicationContext配置文件的路径 ,可使用通配符,多个路径用,号分隔 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/config/applicationContext.xml</param-valu

主流的数据库连接池

1.数据库连接池概述 数据库连接的建立是一种耗时.性能低.代价高的操作,频繁的数据库连接的建立和关闭极大的影响了系统的性能.数据库连接池是系统初始化过程中创建一定数量的数据库连接放于连接池中,当程序需要访问数据库时,不再建立一个新的连接,而是从连接池中取出一个已建立的空闲连接,使用完毕后,程序将连接归还到连接池中,供其他请求使用,从而实现的资源的共享,连接的建立.断开都由连接池自身来管理. 数据库连接池为系统的运行带来了以下优势:昂贵的数据库连接资源得到重用:减少了数据库连接建立和释放的时间开销

谈谈数据库连接池

前言: 最近又在为暑假的实习奔波...今天的面试被问到连接池有没有使用过,一时竟然哑口(简历上写的可以熟悉mysql啊~).回来反思总结了一下,然后又看了20分钟网上视频.为防止下次面试又出糗,于是便有了这篇随笔~ l 为什么使用数据库连接池: 为了避免每次访问数据库的时候都需要重新建立新的连接而影响运行速度,在实际的项目中通常使用数据库连接池来统一调配,从而提高数据库的访问效率. l 原理示意图: l 下载解压 l 用浏览器查看文档doc下的index.html文件 l 常见2种连接方式 (1

[原创]java WEB学习笔记80:Hibernate学习之路--- hibernate配置文件:JDBC 连接属性,C3P0 数据库连接池属性等

本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱好者,互联网技术发烧友 微博:伊直都在0221 QQ:951226918 -----------------------------------------------------------------------------------------------------------------

【转】JDBC学习笔记(8)——数据库连接池(dbcp&amp;C3P0)

转自:http://www.cnblogs.com/ysw-go/ JDBC数据库连接池的必要性 一.在使用开发基于数据库的web程序时,传统的模式基本是按一下步骤: 1)在主程序(如servlet/beans)中建立数据库连接 2)进行sql操作 3)断开数据库连接 二.这种模式开发,存在的问题: 1)普通的JDBC数据库连接使用DriverManager来获取,每次向数据库建立连接的时候都要将Connection加载进内存中,再验证用户名和密码(得花费0.05s~1s的时间).需要数据库连接

java jdbc深入理解(connection与threadlocal与数据库连接池和事务实)

1.jdbc连接数据库,就这样子 Class.forName("com.mysql.jdbc.Driver"); java.sql.Connection conn = DriverManager.getConnection(jdbcUrl); 2.通过传入jdbc url用Drivermanager.getConnection(jdbcurl)连接数据库, 注意:一次Drivermanager.getConnection(jdbcurl)获得只是一个connection,并不能满足高并

在JAVA中实现JDBC数据库连接池

[转自e良师益友网]Java程序员都很羡慕Windows ADO ,只需要new Connection 就可以直接从数据库连接池中返回Connection.并且 ADO Connection 是线程安全的,多个线程可以共用一个Connection,所以ASP程序一般都把getConnection 放在 Global.asa 文件中,在 IIS 启动时建立数据库连接.ADO 的Connection 和Result 都有很好的缓冲,并且很容易使用.推荐学习尚硅谷JDBC视频教程. 其实我们可以自己写