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

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

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

    前段时间,接手一个项目使用的是原始的jdbc作为数据库的访问,发布到服务器上在运行了一段时间之后总是会出现无法访问的情况,登录到服务器,查看tomcat日志发现总是报如下的错误. Caused by: ...

  2. 【JDBC核心】数据库连接池

    数据库连接池 传统模式 使用数据库的传统模式: 在主程序(servlet.beans等)中建立数据库连接: 进行 SQL 操作: 断开数据库连接. 这种模式存在的问题: JDBC 连接数据库的方式(四 ...

  3. JAVA企业级开发-jdbc事务,数据库连接池(10)

    一.   JDBC事务 事务: 问题1.什么是事务 问题2.java中(jdbc)如何控制事务 1. 事务—重点 指的的逻辑上的一组(一组sql,insert update ,delete)操作,组成 ...

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

    <bean id="sqliteDataSource" class="org.apache.tomcat.jdbc.pool.DataSource" de ...

  5. j2ee数据库连接池配置大全

    <!--web.xml begin--> <!--web.xml Spring ApplicationContext配置文件的路径 ,可使用通配符,多个路径用,号分隔 --> ...

  6. Java数据库连接池原理与简易实现

    1.什么是数据库连接池 我们现在在开发中一定都会用到数据库,为了提高我们的系统的访问速度,数据库优化是一个有效的途径.我们现在开发中使用数据库一般都要经历以下的四个步骤:(1)加载数据库的驱动类,(2 ...

  7. 数据库连接池在Tomcat中的几种配置方法

    数据库连接是一种关键的有限的昂贵的资源,这在多用户网页应用程序中体现的尤为突出.对数据库连接的管理能显著影响到整个应用程序的伸缩性和健壮性,影响到程序的性能指标,数据库连接池正是针对这个问题提出的. ...

  8. Spring Boot 数据库连接池参数

    挑战A.I.,赢百万奖金......了解更多详情>>> Tomcat JDBC 连接池 Spring Boot 默认选择 Tomcat JDBC Pool 作为数据库连接池.Tomc ...

  9. JAVA和C#中数据库连接池原理与应用

    JAVA和C#中数据库连接池原理 在现在的互联网发展中,高并发成为了主流,而最关键的部分就是对数据库操作和访问,在现在的互联网发展中,ORM框架曾出不穷, 比如:.Net-Core的EFCore.Sq ...

随机推荐

  1. C#-Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046}

    异常信息如下 捕获到执行这句时异常: Excel.Application ep = new Excel.ApplicationClass(); Retrieving the COM class fac ...

  2. Oracle数据库主外键 级联删除记录

    /** * 1. NO ACTION :指当删除主表中被引用列的数据时,如果子表的引用列中包含该值,则禁止该操作执行. * * 2. SET NULL :指当删除主表中被引用列的数据时,将子表中相应引 ...

  3. Linux下源码编译php7

    1.安装依赖包 yum install -y gcc gcc-c++ make zlib zlib-devel pcre pcre-devel libjpeg libjpeg-devel libpng ...

  4. ARM处理器基础知识

    1.ARM处理器的寄存器,ARM与Thumb状态,7中运行模式 http://blog.chinaunix.net/uid-28458801-id-3494646.html 2.ARM的7种工作模式. ...

  5. Fiddler抓websocket协议的包,用jmeter做并发测试

    1.Fiddler: 左边为ws请求url.右边为请求数据,响应数据 jmeter:

  6. ubuntu坑(持续更新~)

    0.you should not install packages from any repository that represents a different system version to ...

  7. js动画--封装透明度

    这一章我将讲述一些如何将透明度的变化也封装到调用函数中,对于前面几节课的介绍,已经将基本的属性封装到函数中了,宽,长等属性.关于透明度的变化还是有一点区别的,这一章我将封装透明度 先创建一个div & ...

  8. Win10-无法启动虚拟机

    环境:Win10 - 1903 Q1:安装使用Hyper-V后,安装Virtual box新建VM,无法启动 A1:卸载Hyper-V(非必须) 管理员运行ps/cmd   bcdedit /set  ...

  9. Redux的图文模型

    Also these are really nice (from http://slides.com/jenyaterpil/redux-from-twitter-hype-to-production ...

  10. python - django 将图片路径地址转换成 InMemoryUploadedFile 并存储数据库

    # 问题场景:对接第三方时遇到一个图片存储问题,对方给的是他们服务器的图片路径地址,但是 我这里存储图片用的是 ImageField  字段属性,也设置了存储路径,现在一旦将图片显示到前端就会将设置的 ...