需要 Oracle JDBC 驱动程序的数据源并启用/禁用 CacheConnection
有Oracle 11g2和基于spring的应用Java
需要在运行时启用/禁用 CacheConnection 功能的 Oracle JDBC 驱动程序的数据源 - 即,如果 CacheConnection 启用,则不会建立新连接,如果数据源中有空闲连接,如果 CacheConnection 禁用,则始终建立新连接,并且存在在闲置后关闭
早期我们使用 apache DataSource:
<bean id="datasourceClassic" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.OracleDriver" />
<property name="url" value="jdbc:oracle:oci:@TEST" />
<property name="username" value="TEST" />
<property name="password" value="TEST" />
<property name="maxActive" value="10" />
<property name="defaultAutoCommit" value="false" />
</bean>
-- 但这个数据源没有启用/禁用 CacheConnection 的功能
我们测试了 OracleDataSource
:
<property name="URL" value="jdbc:oracle:oci:@TEST" />
<property name="user" value="TEST" />
<property name="password" value="TEST" />
<property name="connectionCachingEnabled" value="true" />
<property name="connectionCacheProperties">
<props>
<prop key="MinLimit">0</prop>
<prop key="MaxLimit">1</prop>
<prop key="InitialLimit">0</prop>
<prop key="InactivityTimeout">10</prop>
<prop key="ConnectionWaitTimeout">10</prop>
<prop key="ValidateConnection">true</prop>
</props>
</property>
</bean>
-- 具有此功能,但仅当在 context.xml 中显式设置此选项且方法 setConnectionCachingEnabled
从运行时弃用时才有效
Oracle 自 11g2 起推荐使用通用连接池 (UCP):
<bean id="datasource2" class="oracle.ucp.jdbc.PoolDataSourceFactory" factory-method="getPoolDataSource">
<property name="connectionFactoryClassName" value="oracle.jdbc.pool.OracleDataSource"/>
<property name="connectionPoolName" value="TEST"/>
<property name="URL" value="jdbc:oracle:oci:@TEST" />
<property name="user" value="TEST" />
<property name="password" value="TEST" />
<property name="initialPoolSize" value="0" />
<property name="minPoolSize" value="0" />
<property name="maxPoolSize" value="1" />
<property name="validateConnectionOnBorrow" value="true" />
</bean>
- 但该池也没有启用/禁用 CacheConnection 的功能...
知道什么 DataSource 可以满足我的要求吗?
更新:
我使用 org.apache.commons.dbcp.BasicDataSource 测试下一个 trik:
/* "Disable" CacheConnection */
BasicDataSource bds = ... //get DataSource
bds.setMaxIdle(0);
和:
/* "Enable" CacheConnection */
BasicDataSource bds = ... //get DataSource
bds.setMaxIdle(bds.getMaxActive());
工作方式类似于
There are Oracle 11g2 and application Java based of spring
Need of DataSource for oracle JDBC driver with enable/disable CacheConnection functional in runtime - i.e. if CacheConnection is enable new connection not established if there are free connection in DataSource, if CacheConnection is disable always new connection established and existence is closing after put in idle
Early we used apache DataSource:
<bean id="datasourceClassic" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.OracleDriver" />
<property name="url" value="jdbc:oracle:oci:@TEST" />
<property name="username" value="TEST" />
<property name="password" value="TEST" />
<property name="maxActive" value="10" />
<property name="defaultAutoCommit" value="false" />
</bean>
-- but this data source don't have functional for enable/disable CacheConnection
We tested OracleDataSource
:
<property name="URL" value="jdbc:oracle:oci:@TEST" />
<property name="user" value="TEST" />
<property name="password" value="TEST" />
<property name="connectionCachingEnabled" value="true" />
<property name="connectionCacheProperties">
<props>
<prop key="MinLimit">0</prop>
<prop key="MaxLimit">1</prop>
<prop key="InitialLimit">0</prop>
<prop key="InactivityTimeout">10</prop>
<prop key="ConnectionWaitTimeout">10</prop>
<prop key="ValidateConnection">true</prop>
</props>
</property>
</bean>
-- have this functional but it work only if explicity set up this options in context.xml, and method setConnectionCachingEnabled
depreceted from runtime
Oracle since 11g2 recomended use Universal Connection Pool (UCP):
<bean id="datasource2" class="oracle.ucp.jdbc.PoolDataSourceFactory" factory-method="getPoolDataSource">
<property name="connectionFactoryClassName" value="oracle.jdbc.pool.OracleDataSource"/>
<property name="connectionPoolName" value="TEST"/>
<property name="URL" value="jdbc:oracle:oci:@TEST" />
<property name="user" value="TEST" />
<property name="password" value="TEST" />
<property name="initialPoolSize" value="0" />
<property name="minPoolSize" value="0" />
<property name="maxPoolSize" value="1" />
<property name="validateConnectionOnBorrow" value="true" />
</bean>
-- but this pool also didn't have functional to enable/disable CacheConnection...
Any idea of what DataSource can satisfy my requirements?
UPDATE:
I test next trik with org.apache.commons.dbcp.BasicDataSource
:
/* "Disable" CacheConnection */
BasicDataSource bds = ... //get DataSource
bds.setMaxIdle(0);
and:
/* "Enable" CacheConnection */
BasicDataSource bds = ... //get DataSource
bds.setMaxIdle(bds.getMaxActive());
Works like <property name="connectionCachingEnabled" value="true" />
- what do you think about this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否能够创建一个启用缓存的数据源和一个禁用缓存的数据源?如果是这样,您可以在 ApplicationContext 中创建两个具有不同名称和配置的不同 DataSource bean。您可以在应用程序中自动装配两者并在它们之间动态切换。
或者,如果您需要已连接数据源的 bean,则可以创建两个 ApplicationContext 并在它们之间切换。请注意,后一种解决方案会阻止结果缓存;我不确定第一个解决方案对缓存的影响。
Are you able to create a data source which has caching enabled and one which has caching disabled? If so, you could create two different DataSource beans with different names and configurations in your ApplicationContext. You could autowire both in your application and switch between them dynamically.
Alternately, if you need beans with the DataSource wired already, you could create two ApplicationContexts and toggle between them. Note that the latter solution prevents results caching; I'm not sure about the effects on caching with the first solution.