📄 ccache7.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 the connection or not. There are 3 connection cache time-out: TimeToLive timeout Inactivity timeout FixedWait timeout This file is a sample to illustrate Inactivity timeout using XAConnection. Inactivity time-out is a period of inactivity for a given connection. The physical connection will be closed after the specified period of inactivity. The default value is -1, which means timeout thread won't reclaim the corresponding resources (the physical connection will remain if middle tier or application does not explicitly call close() even its logical connectin is closed long time ago) Please refer to CCache4.java, CCache5.java, and CCache6.java for other timeout features. **/ import java.sql.*;import javax.sql.*;import oracle.jdbc.*;import oracle.jdbc.pool.*;class CCache7 extends Thread{ OracleXAConnectionCacheImpl ods = null; public static void main (String args[]) throws SQLException { CCache7 cc7 = new CCache7 (); cc7.start (); } public void run () { dynamicInactive (); fixedReturnNullInactive(); fixedwaitInactive (); defaultInactive (); } // end of public main (...) private void setods () { // make an oracle connection cache instance try { ods = new OracleXAConnectionCacheImpl(); } catch (SQLException e) { e.printStackTrace(); } 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) { } ods.setURL (url); ods.setUser ("hr"); ods.setPassword ("hr"); try { ods.setMaxLimit (3); } catch (SQLException e) { e.printStackTrace(); } } // // combination with DYNAMIC_SCHEME. // Maxlimit set to 3 but 5 connections can be obtained at the // same time. // // 3 inactive (closed) connections will be reclaimed // private void dynamicInactive () { setods (); try { ods.setCacheScheme (OracleConnectionCacheImpl.DYNAMIC_SCHEME); // Note, threadWakeUpInterval should be less than Inactivity Timeout ods.setThreadWakeUpInterval (1); ods.setCacheInactivityTimeout (5); } catch (SQLException e) { e.printStackTrace(); } System.out.println("\nCombine Inactivity Timeout with DYNAMIC_SCHEME"); System.out.println("Max Limit size for cache is 3,\n" + "but 5 connections can be obtained\n"); Connection[] conn = new Connection[5]; for ( int i = 0; i < 5; ++i ) { try { conn[i] = ods.getConnection(); } catch (SQLException e) { 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 // Make 3 connections inactive by closing these 3 logical // connections and return them to cache try { conn[0].close(); conn[1].close(); conn[2].close(); } catch (SQLException e) { e.printStackTrace(); } // sleep for 10 seconds, so timeout thread can close the // physical connections in cache try { System.out.println("Sleeping for 10 seconds..."); Thread.sleep (10000); } catch (java.lang.InterruptedException ie) {} System.out.println("After timeout thread played its role..."); System.out.println("Active size : " + ods.getActiveSize()); // expect 2 System.out.println("Cache Size : " + ods.getCacheSize()); // expect 2 try { conn[3].close(); conn[4].close(); ods.close(); ods = null; } catch (SQLException e) { e.printStackTrace(); } } // // combination with FIXED_RETURN_NULL_SCHEME // Maxlimit set to 3 but 5 connections are tried at the same time // with 3 success and 2 fail. // private void fixedReturnNullInactive( ) { setods (); try { ods.setCacheScheme (OracleConnectionCacheImpl.FIXED_RETURN_NULL_SCHEME); ods.setThreadWakeUpInterval (1); // timeout thread wake ups every 1 second ods.setCacheInactivityTimeout (5); // 5 seconds } catch (SQLException e) { e.printStackTrace(); } System.out.println("\nCombine Inactivity Timeout and FIXED_RETURN_NULL_SCHEME"); System.out.println("Max Limit size for cache is 3, 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) { 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("After connection creation."); System.out.println("Active size : " + ods.getActiveSize()); // expect 3 System.out.println("Cache Size : " + ods.getCacheSize()); // expect 3 for (int i = 0; i < 5; ++i) if ( conn[i] != null ) try { conn[i].close(); } catch (SQLException e) { e.printStackTrace(); } // 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("After all the logical connections get closed,\n" + "all the physical connections are closed without " + "explicit calling to close."); System.out.println("Active size : " + ods.getActiveSize()); // expect 0 System.out.println("Cache Size : " + ods.getCacheSize()); // expect 0 try { ods.close(); ods = null; } catch (SQLException e) { e.printStackTrace (); } } // end of fixedReturnNullInactive () // // Combine Inactivity time out with FIXED_WAIT_SCHEME // Maxlimit set to 3 but 5 connections are tried at the same time. // (using threads) with 3 succeed at ealier time, and 2 succeed // about 10 seconds later. // // One connection will be made inactive (closed) when the // activeSize reaches Maxlimit 3. // private void fixedwaitInactive () { setods (); try { ods.setCacheScheme (OracleConnectionCacheImpl.FIXED_WAIT_SCHEME); ods.setThreadWakeUpInterval (1); // Timeout Thread wakes up every second ods.setCacheInactivityTimeout (5); // physical connection will be closed 5 seconds later // after its logical connection get closed } catch (SQLException e) { e.printStackTrace(); } System.out.println("\nCombine Inactivity Timeout and FIXED_WAIT_SCHEME"); System.out.println("Max Limit size for cache is 3,\n" + "but 5 connections are tried. 3 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(); try { System.out.println("Sleeping for 10 seconds..."); Thread.sleep (10000); } catch (java.lang.InterruptedException ie) {} // closed above MaxLimit connections to let other threads // get connection int connkilled = 0; while ( (FixedWaitThread.getthreadnum() >= 4) && (connkilled <= 1) ) { if ( FixedWaitThread.getconnum() >= 3 ) { if ( t1.getconnected() ) t1.closeconnection(); else if ( t2.getconnected() ) t2.closeconnection(); else if ( t3.getconnected() ) t3.closeconnection(); else if ( t4.getconnected() ) t4.closeconnection(); else if ( t5.getconnected() ) t5.closeconnection(); ++connkilled; } } // end of while loop // close all the logical connections t1.closeconnection(); t2.closeconnection(); t3.closeconnection(); t4.closeconnection(); t5.closeconnection(); // wait for all threads to end try { t1.join (); t2.join (); t3.join (); t4.join (); t5.join (); } catch (Exception e) { e.printStackTrace(); } try { Thread.sleep (10000); } catch (InterruptedException ie) {} System.out.println("Sleeping for 10 seconds, so timeout thread close " + "all the physical connections.\nCache Size: " + ods.getCacheSize()); // expect 0 try { ods.close(); ods = null; } catch (SQLException e) { e.printStackTrace(); } } // end of fixedwaitInactive () // // At default setting (Inactivity time out takes -1), the physical connection // will remain even its correspondind logical connection got closed long time // ago // private void defaultInactive () { setods (); try { ods.setCacheScheme (OracleConnectionCacheImpl.DYNAMIC_SCHEME); ods.setThreadWakeUpInterval (1); } catch (SQLException e) { e.printStackTrace(); } System.out.println("\nUsing default Inactivity time out value -1"); Connection conn = null; try { conn = ods.getConnection(); } catch (SQLException e) { e.printStackTrace(); } 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 // Make the connection inactive by closing it try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } // sleep for 20 seconds, so timeout thread can close the // physical connections in cache try { System.out.println("Sleeping for 20 seconds..."); Thread.sleep (20000); } catch (java.lang.InterruptedException ie) {} System.out.println("After closing the logical connection and waiting,\n" + "timeout thread taking place, physical connection " + "still there"); System.out.println("Active size : " + ods.getActiveSize()); // expect 0 System.out.println("Cache Size : " + ods.getCacheSize()); // expect 1 try { ods.close(); ods = null; } catch (SQLException e) { e.printStackTrace(); } }} // end of class CCache7
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -