服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

[oracle]使用oracle10g jdbc 驱动中connectioncaching所产生的问题

隐含连接缓存
在oracle 数据库10g 之前,jdbc 驱动程序可提供连接缓存支持,然而
在可扩展性、易用性和可管理性方面存在诸多限制。例如,所有对象均在
同一数据库中,并作为同一用户进行鉴权,而没有清除失效连接的机制;
此外,由于所有对话均属于同一数据库,并作为同一用户进行鉴权,因此
根本无法存储作为其它用户进行鉴权的连接。pre-10g jdbc 驱动程序缓存
管理不允许连接缓存在可能失效时调整大小或刷新;不支持搜索连接或收
回所放弃的连接。这些缺点促使了对一种更完善的新型连接缓存机制“隐含
连接缓存”的需求。
隐含连接缓存可提供丰富的特性,包括:透明或隐含访问连接缓存,支持
存储任何经过鉴权的连接,能够刷新或重复利用来自缓存的失效连接,支
持基于用户定义的属性的连接检索、基于属性和权重的连接检索。

透明访问连接缓存
通过缺省设置,datasources 允许获得直接到数据库的物理连接。利用“隐
含连接缓存“,通过简单将datasource 属性connectioncachingenabled
指向true,便可从连接缓存获得连接,始终使用同一标准getconnection()
api。这极大简化了datasource 和连接缓存访问。其它所有事项均可选,
并可根据需求来执行。例如,可以选定一组定义缓存行为的连接缓存属性,
而不必使用缺省属性。

// example to show binding of oracledatasource to jndi
// with relevant cache properties set on the datasource.

// set datasource properties
ods.setuser(“scott”);

ods.setconnectioncachingenabled(true);
ods.setconnectioncachename(“mycache”);
ods.setconnectioncacheproperties(cp);
ctx.bind(“myds”, ods);

ods =(oracledatasource) ctx. lookup(“myds”); // lookup cache datasource
//transparent creation and retrieval of connection(s) from “mycache”
conn = ods.getconnection();
conn.close(); // return connection to the cache

感谢oracle10g jdbc driver带来的令人激动人心的特性,但是在实际的使用过程中,出现了下列问题:
问题描述: 在使用了setconnectioncachingenabled(true); 语句后,我们发现当程序在cache模式运行下
,如果这个时候对数据库中的plsql的内容进行更改后,运行程序访问这个plsql的话,会报出如下错误:
(csdn如何上传图片,faint)依次为ora-06512,ora-0650,ora-04065,ora-04061,ora-04068,最后一个错误是package的状态已经被废弃。

经过调查,发现
1。如果不使用cache,不会出现这个问题。
2。使用cache,这个问题会在不同的server上面在不同的一段时间间隔内自动消失。
3。如果不使用cache,也无法使用oracle提供的连接池功能。如最小的连接数,初始化时所建立的连接数等。
4。不是所有的plsql的内容变更后都出现这个问题,只有plsql中包含有db link的plsql才存在此问题。

解决篇
根据2。使用cache,这个问题会在不同的server上面在不同的一段时间间隔内自动消失。
这个特性,从cache的设定参数上进行测试,终于发现 解决方法。
解决方法:
   这段是oracle的官方文档
connection recycling support
over a period of time, connection cache accumulates stale connections. there are two modes for recycling or refreshing stale connections in the cache: refresh_invalid_connections and refresh_all_connections.
• with refresh_invalid_connections each pooledconnection in the cache is checked3; if invalid, the connection’s resources are removed and replaced with a new pooledconnection.
• with refresh_all_connections, all the available connections in the cache are closed and replaced with new valid physical connections.

3 the validity test is as simple as: select 1 from dual;

程序中的设定
在从data source中取得db连接之前调用如下这段话即可
  oracleconnectioncachemanager.refreshcache(cache名,oracle.jdbc.pool.oracleconnectioncachemanager.refresh_invalid_connections);

总结: 新技术伴随着新风险, 莫名其妙的问题虽然解决了,但是 为什么是dblink,这其中的具体的产生原因又是什么呢?

扫描关注微信公众号