📄 ccache3.java
字号:
/** * JDBC 2.0 Spec doesn't mandate that JDBC vendors implement a * Connection Cache. However, we implemented a basic one with 2 * schemes as an Example. * * There are 3 cache schemes: DYNAMIC_SCHEME (default) * FIXED_RETURN_NULL_SCHEME * FIXED_WAIT_SCHEME * * A Sample demo to illustrate FIXED_WAIT_SCHEME of * OracleConnectionCacheImpl. * Fixed with Wait : At no instance there will be no more active * connections than the the Maximum limit. Request for new connections * beyond the max limit will wait until other connections are relieved. * Please compare with CCache1.java and CCache2.java * Please use jdk1.2 or later version * * usage: java CCache3 * java CCache3 <number of threads> */import java.sql.*;import oracle.jdbc.OracleStatement;import oracle.jdbc.pool.*;import java.util.Random;public class CCache3 extends Thread{ // Default no of threads to 10 private static int NUM_OF_THREADS = 10; int m_myId; static int c_nextId = 1; static Connection s_conn = null; static boolean share_connection = false; static Random rd = null; static OracleConnectionPoolDataSource ocpds = null; static OracleConnectionCacheImpl ods = null; synchronized static int getNextId() { return c_nextId++; } public static void main (String args []) { try { /* Load the JDBC driver */ DriverManager.registerDriver(new oracle.jdbc.OracleDriver()); String url = "jdbc:oracle:oci8:@"; 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 } ods = new OracleConnectionCacheImpl(); ods.setURL(url); ods.setUser("hr"); ods.setPassword("hr"); ods.setMaxLimit (3); ods.setCacheScheme (OracleConnectionCacheImpl.FIXED_WAIT_SCHEME); rd = new Random(12345); if (args.length > 1) { System.out.println("Usage: java CCache3 <no of Threads>\n java CCache3"); System.exit(0); } else if (args.length == 1) { NUM_OF_THREADS = Integer.parseInt (args[0]); } // Create the threads Thread[] threadList = new Thread[NUM_OF_THREADS]; // spawn threads for (int i = 0; i < NUM_OF_THREADS; i++) { threadList[i] = new CCache3(); threadList[i].start(); } // Start everyone at the same time setGreenLight (); // wait for all threads to end for (int i = 0; i < NUM_OF_THREADS; i++) { threadList[i].join(); } } catch (Exception e) { e.printStackTrace(); } } public CCache3() { super(); // Assign an Id to the thread m_myId = getNextId(); } public void run() { Connection conn = null; ResultSet rs = null; Statement stmt = null; while (!getGreenLight()) yield(); try { for (int i =0; i < 10; ++i) { System.out.println("Thread " + m_myId + " About to start " + i + " th iteration"); if (rd != null) { int sltp = (Math.abs(rd.nextInt())) % 30; System.out.println("Sleeping for " + sltp + " secs."); sleep (sltp); } if (ods == null) System.out.println("ods is null"); // Get the connection System.out.println("Thread " + m_myId + " Active Size is " + ods.getActiveSize()); System.out.println("Thread " + m_myId + " Cache Size is " + ods.getCacheSize()); System.out.println("Thread " + m_myId + " About to get Connection "); conn = ods.getConnection("hr","hr"); System.out.println("Thread " + m_myId + " Got Connection "); stmt = conn.createStatement (); // Create a Statement // Yield to other threads yield(); // Execute the Query rs = stmt.executeQuery ("select USER from dual"); // Loop through the results while (rs.next()) { System.out.println("Thread " + m_myId + " User Name : " + rs.getString(1)); yield(); // Yield To other threads } // Close all the resources rs.close(); rs = null; // Yield to other threads yield(); // Close the statement stmt.close(); stmt = null; System.out.println("Thread " + m_myId + " Closing connection"); conn.close(); conn = null; System.out.println("Thread " + m_myId + " Done with " + i + " th iteration"); } } catch (Exception e) { System.out.println("Thread " + m_myId + " got Exception: " + e); e.printStackTrace(); return; } System.out.println("Thread " + m_myId + " is finished. "); } static boolean greenLight = false; static synchronized void setGreenLight () { greenLight = true; } synchronized boolean getGreenLight () { return greenLight; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -