智能的连接缓存隐式连接缓存-用Java、JDBC和Web服务进行Oracle 数据库 10g程序设计

时间:2024-05-15 02:20:12
【文件属性】:

文件名称:智能的连接缓存隐式连接缓存-用Java、JDBC和Web服务进行Oracle 数据库 10g程序设计

文件大小:372KB

文件格式:PPT

更新时间:2024-05-15 02:20:12

Java JDBC

智能的连接缓存: 隐式连接缓存 缓存管理器 Oracle JDBC 驱动程序 ... 会话 1.1 会话 n.1 .... 缓存 1 会话 1.m .... 缓存 n 会话 n.m J2EE 服务器(例如,OracleAS 10g) DataSource Properties ConnectionCachingEnabled: This property is for enabling the connection cache. By default, this property is set to FALSE. The default DataSource behavior, is to retrieve physical connections to the database. If connection cache access is desired, set this property to TRUE. The getConnectionCachingEnabled() method returns TRUE or FALSE depending on whether connection caching is turned on or not. ConnectionCacheName: This property sets the name of the cache. A cache name is unique and is used by the connection cache manager to identify the cache. A default cache name will be provided if a name is not set for a cache. The getConnectionCacheName()method returns the name of the cache. ConnectionCacheProperties: This property is optional and allows creation of the connection cache, based on the properties specified. If caching is enabled without setting this property, then a cache is created with default values. getConnectionCacheProperties()method returns the properties on the cache. The code shown below creates a DataSource, ods, and initializes with cache specific properties. This DataSource is bound with the name DS. You can lookup for this object from JNDI and use for obtaining connections from the cache. The steps involved in accessing the cache are: Lookup a DataSource by name, DS, from JNDI and obtain the DataSource object Invoke getConnection() method on the DataSource After using the connection, return the connection back to the cache for reuse by invoking the close() method. import oracle.jdbc.pool.*; Context ctx = new IntialContext(ht); OracleDataSource ods = new OracleDataSource(); ods.setUser("hr"); ods.setConnectionCachingEnabled(True); ods.setConnectionCacheName("cache1"); ods.setConnectionCacheProperties(cp); ctx.bind("DS", ods); ... //lookup DataSource from the cache ods = (OracleDataSource)ctx.lookup("DS"); conn = ods.getConnection(); conn.close(); ods.close(); A cache can be explicitly removed via the connection cache manager or is removed when the DataSource is closed as shown in the last line of the code. Calling close()on the cache enabled DataSource, closes the underlying cache and frees all the resources. You can invoke this method when the cache will not be used any more.


网友评论