📄 ccache4.java
字号:
/** Oracle JDBC supports a JDBC connection cache time-out scheme. The intent is to unconditionally reclaim connections. This guarantees the reclamation of connection resources no matter middle tier or application explicitly close its connections or not. There are 3 connection cache time-out: TimeToLive timeout Inactivity timeout FixedWait timeout This file is a sample to illustrate TimeToLive timeout. TimeToLive timeout is a slice of time given to a connection. When this time-out expires, the logical connection is closed and is put back to the cache. It will have different effects when combined with 3 different schems (DYNAMIC_SCHEME, FIXED_RETURN_NULL_SCHEME, FIXED_WAIT_SCHEME). The default value for TTL is -1, which means timeout thread won't reclaim corresponding resources (both logical and physical connections will remain if the middle tier or application donot close the connection explicitly) Please refer to CCache5.java and CCache6.java, CCache7.java for other timeout features. **/ import java.sql.*;import javax.sql.*;import oracle.jdbc.*;import oracle.jdbc.pool.*;class CCache4 extends Thread{ OracleConnectionCacheImpl ods = null; public static void main (String args[]) throws SQLException { CCache4 cc4 = new CCache4 (); cc4.start (); } public void run () { dynamicTTL (); fixedReturnNullTTL (); fixedwaitTTL (); defaultTTL (); } private void setods () { String url = "jdbc:oracle:oci:@"; // thin driver can be used as well try { String url1 = System.getProperty("JDBC_URL"); if (url1 != null) url = url1; } catch (Exception e) { // If there is any security exception, ignore it // and use the default } // make an oracle connection cache instance try { ods = new OracleConnectionCacheImpl(); ods.setURL(url); ods.setUser("hr"); ods.setPassword("hr"); //MinLimit defaults to 0 ods.setMaxLimit (3); } catch (SQLException e) { e.printStackTrace(); } } // // Combine TTL with DYNAMIC_SCHEME. // Maxlimit set to 3 but 5 connections can be obtained at the same time // Without explicitly calling to close(), all 5 active connections will // be closed after TTL. And physical connections will shrink to MaxLimit. // private void dynamicTTL () { setods(); // make an oracle connection cache instance try { ods.setCacheScheme("DYNAMIC_SCHEME"); ods.setThreadWakeUpInterval(1); ods.setCacheTimeToLiveTimeout(5); } catch (SQLException e) { e.printStackTrace(); } System.out.println("\nCombine TTL and DYNAMIC_SCHEME"); System.out.println("Max Limit size for cache is 3, \n" + "but 5 connections can be obtained\n"); System.out.println("Before making any connections"); System.out.println("Active size : " + ods.getActiveSize()); // expect 0 System.out.println("Cache Size : " + ods.getCacheSize()); // expect 0, as the default MinLimit Connection[] conn = new Connection[5]; for ( int i = 0; i < 5; ++i ) { try { // get a logical connection from the Cache. conn[i] = ods.getConnection(); } catch (SQLException e) { System.out.println("Failed: "); e.printStackTrace(); } if ( conn[i] != null ) System.out.println("Succeed at connection " + (i+1)); else System.out.println("Failed at connection " + (i+1)); } System.out.println("After making 5 connections"); System.out.println("Active size : " + ods.getActiveSize()); // expect 5 System.out.println("Cache Size : " + ods.getCacheSize()); // expect 5 // sleep for 10 seconds, so timeout thread can close the active connections try { System.out.println("Sleeping for 10 seconds..."); Thread.sleep (10000); } catch (java.lang.InterruptedException ie) {} System.out.println("Without explicit calling to close()\n" + "all the logical connections are closed " + "and physical connections shrink to MaxLimit"); System.out.println("Active size : " + ods.getActiveSize()); // expect 0 System.out.println("Cache Size : " + ods.getCacheSize()); // expect 3 // close all the connections in the cache and free all the resources for (int i = 0; i < 5; ++i) { if ( conn[i] != null ) try { conn[i].close(); } catch (SQLException e) { } } try { ods.close(); ods = null; } catch (SQLException e) { e.printStackTrace(); } } // end of dynamicTTL () // // Combine TTL with FIXED_RETURN_NULL_SCHEME // Maxlimit set to 3 but 5 connections are tried at the same time // with 3 success and 2 fail. All 3 active connections will be closed // Without explicit calling to close() // private void fixedReturnNullTTL() { setods (); try { ods.setCacheScheme(OracleConnectionCacheImpl.FIXED_RETURN_NULL_SCHEME); ods.setThreadWakeUpInterval (1); // timeout thread wakes up every 1 second ods.setCacheTimeToLiveTimeout (5); } catch (SQLException e) { e.printStackTrace(); } System.out.println("\nCombine TTL and FIXED_RETURN_NULL_SCHEME"); System.out.println("Max Limit size for cache is 3, \n" + "but 5 connections are tried with 3 success and 2 fail"); Connection[] conn = new Connection[5]; for ( int i = 0; i < 5; ++i ) { try { // get a logical connection from the Cache. conn[i] = ods.getConnection(); } catch (SQLException e) { System.out.println("Failed: "); e.printStackTrace(); } if ( conn[i] != null ) System.out.println("Succeed at connection " + (i+1)); else System.out.println("Expected Fail at connection " + (i+1)); } System.out.println("Active size : " + ods.getActiveSize()); // expect 3 System.out.println("Cache Size : " + ods.getCacheSize()); // expect 3 // sleep for 10 seconds, so timeout thread can close the active connections try { System.out.println("Sleeping for 10 seconds..."); Thread.sleep (10000); } catch (java.lang.InterruptedException ie) {} System.out.println("Without calling to connection.close(), 3 logical " + "connections are closed"); System.out.println("Active size : " + ods.getActiveSize()); // expect 0 System.out.println("Cache Size is " + ods.getCacheSize()); // expect 3 try { ods.close(); ods = null; } catch (SQLException e) { e.printStackTrace(); } } // end of fixedReturnNullTTL () // // Combine TTL with FIXED_WAIT_SCHEME // Maxlimit set to 3 but 5 connections are tried at the same time. // Some get success with ealier timestamp and other get success with // 10 seconds later timestamp (waiting for timeout thread working) // private void fixedwaitTTL() { setods (); try { ods.setCacheScheme(OracleConnectionCacheImpl.FIXED_WAIT_SCHEME); ods.setThreadWakeUpInterval (1); // timeout thread wakes up every 1 second ods.setCacheTimeToLiveTimeout (5); } catch (SQLException e) { e.printStackTrace(); } System.out.println("\nCombine TTL and FIXED_WAIT_SCHEME"); System.out.println("Max Limit size for cache is 3, " + "but 5 connections are tried. \n3 success with " + "ealier timestamp,\n2 success " + "with about 10 seconds later timestamp"); FixedWaitThread t1 = new FixedWaitThread (ods, 1); FixedWaitThread t2 = new FixedWaitThread (ods, 2); FixedWaitThread t3 = new FixedWaitThread (ods, 3); FixedWaitThread t4 = new FixedWaitThread (ods, 4); FixedWaitThread t5 = new FixedWaitThread (ods, 5); t1.start(); t2.start(); t3.start(); t4.start(); t5.start(); // start everyone at the same time FixedWaitThread.setGreenLight(); System.out.println("Main thread sleep 10 seconds"); try { Thread.sleep (10000); //Thread.yield (); } catch (java.lang.InterruptedException ie) { } t1.closeconnection(); t2.closeconnection(); t3.closeconnection(); t4.closeconnection(); t5.closeconnection(); try { t1.join(); t2.join(); t3.join(); t4.join(); t5.join(); ods.close(); ods = null; } catch (Exception e) { e.printStackTrace(); } } // end of fixedwaitTTL() // // without set TTL, timeout thread will reclaim nothing if do not close // the connection explicitly // private void defaultTTL () { Connection conn = null; setods (); try { ods.setCacheScheme(OracleConnectionCacheImpl.DYNAMIC_SCHEME); ods.setThreadWakeUpInterval (1); // timeout thread wakes up every 1 second conn = ods.getConnection(); } catch (SQLException e) { e.printStackTrace(); } System.out.println("\nDefault value for Time To Live is -1"); if ( conn != null ) System.out.println("Succeed at connection"); else System.out.println("Failed at connection"); System.out.println("After making 1 connection"); System.out.println("Active size : " + ods.getActiveSize()); // expect 1 System.out.println("Cache Size : " + ods.getCacheSize()); // expect 1 // sleep for 10 seconds, so timeout thread can close the active connections try { System.out.println("Sleeping for 10 seconds..."); Thread.sleep (10000); } catch (java.lang.InterruptedException ie) {} System.out.println("both logical and physical connections are still there"); System.out.println("Active size : " + ods.getActiveSize()); // expect 1 System.out.println("Cache Size : " + ods.getCacheSize()); // expect 1 try { conn.close(); conn = null; ods.close(); ods = null; } catch (SQLException e) { e.printStackTrace(); } } // end of defaultTTL ()} // end of class CCache4
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -