⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ccache6.java

📁 Java示例100
💻 JAVA
字号:
/**   Oracle JDBC supports a JDBC connection cache time-out   scheme.  There are 3 connection cache time-out:         TimeToLive timeout         Inactivity timeout         FixedWait  timeout   This file is a sample to illustrate FixedWait timeout.   FixedWait timeout is the period between the time a connection   request is made to the server and till the time a connection   has been obtained. A time-out exception is thrown if a connection   is not obtained in the specified time period. This avoids    indefinite waiting.   Its default value of FixedWaitTimeout is -1, which means indefinite   waiting    This feature must be combined with FIXED_WAIT_SCHEME.   Please refer to CCache4.java,  CCache5.java, and CCache7.java for   other timeout features. **/ import java.sql.*;import javax.sql.*;import oracle.jdbc.*;import oracle.jdbc.pool.*;class CCache6{    static OracleConnectionCacheImpl ods = null;    public static void main (String args[]) throws SQLException    {        // make an oracle connection cache instance        ods = new OracleConnectionCacheImpl();        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        }        ods.setURL (url);        ods.setUser ("system");        ods.setPassword ("manager");        ods.setMinLimit (1);        ods.setMaxLimit (2);        ods.setCacheScheme(OracleConnectionCacheImpl.FIXED_WAIT_SCHEME);        ods.setCacheFixedWaitTimeout (5);        System.out.println("\nFixed Wait Timeout demo:\n" +                           "set MaxLimit to 2, and FixedWaitTimeout set to " +                           ods.getCacheFixedWaitTimeout () + " seconds");        System.out.println("Before getting any connection:\n" +                           "Total number of ActiveConnection : " +                           ods.getActiveSize ());  // expect 0        System.out.println("Cache Size: " + ods.getCacheSize());                           // expect 1, as the MinLimit         System.out.println("Total number of connection open(V$SESSION):"                           + checkNoSessions ());  // expect 0        FixedWaitThread t1 = new FixedWaitThread (ods, 1);        FixedWaitThread t2 = new FixedWaitThread (ods, 2);        FixedWaitThread t3 = new FixedWaitThread (ods, 3);        FixedWaitThread t4 = new FixedWaitThread (ods, 4);        t1.start();        t2.start();        t3.start();        t4.start();        FixedWaitThread.setGreenLight(); // start every thread at the same time        try        {            t1.join ();            t2.join ();            t3.join ();            t4.join ();        } catch (Exception e)        {            e.printStackTrace();        }        System.out.println("Main: All threads completed. 2 failed connections" +                           " are expected");        System.out.println("ActiveConnection size : " + ods.getActiveSize ());                          // expect 2        System.out.println("Cache size : " + ods.getCacheSize ()); // expect 2        t1.closeconnection ();        t2.closeconnection ();        t3.closeconnection ();        t4.closeconnection ();        System.out.println("Main: sleeping for 10 sec...");        try        {            Thread.sleep (10000);        } catch (java.lang.InterruptedException ie) {}        System.out.println("\nAfter closing all the logical connections\n" +                           "Total number of ActiveConnection : " +                           ods.getActiveSize ()); // expect 0        System.out.println("Cache size : " + ods.getCacheSize ()); // expect 2        System.out.println("Total number of connection open(V$SESSION):"                           + checkNoSessions ()); // expect 1         try        {            ods.close();            System.out.println("Cache size : " + ods.getCacheSize ());                       // expect 0 because explicit calling to close        } catch (SQLException e)        {            e.printStackTrace();        }     } // end of public main (...)      private static int checkNoSessions ()      {         Connection conn = null;         PreparedStatement pstmt = null;         ResultSet rs = null;         int sessions = 0;         try         {             conn = ods.getConnection();  // Fixed Wait timeout elapsed             pstmt = conn.prepareStatement (                         "SELECT COUNT(username) " +                         "FROM v$session " +                         "WHERE type != \'BACKGROUND\'");             rs = pstmt.executeQuery ();             rs.next ();             sessions = rs.getInt (1);             // subtracting the entry of system/manager from the actual             // entry             --sessions;             rs.close();             rs = null;             pstmt.close();             pstmt = null;             conn.close ();             conn = null;         } catch (SQLException e)         {             e.printStackTrace();         }         return sessions;     } // end of private int checkNoSessions ()  } // end of class CCache6 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -