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

📄 understandcacheattributes.java

📁 数据库连接包括连接池。很有用的资料
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    // Execute a Sample Query in the database to demonstrate that the
    // connection is established
    executeQuery(conn);

    System.out.println();
    System.out.println("-- Listing Connection Cache Attributes");

    // Print the Connection Attributes onto the console
    printAttributes(conn.getConnectionAttributes());

    // Release the connection back to the MyCache
    conn.close();
  }


  /**
   * This method retrieves the connection after passing new
   * parameters.
   *
   * @exception Exception In Case of an Exception
   */
  private void getConnectionWithNewAttributes() throws Exception{
    System.out.println("\n-------------- Invoking method "
      + "getConnectionWithNewAttributes() ---------------");

    System.out.println("-- Initializing New Set of Attributes. The new Attributes are");
    System.out.println("-- CONNECTION_NAME_NEW = OTN_CONNECTION_NEW");
    System.out.println("-- NLS_LANG = ISO-LATIN-1");
    System.out.println();

    // Initialize new Connection Attributes
    java.util.Properties connAttr = new Properties();
    connAttr.setProperty("CONNECTION_NAME_NEW", "OTN_CONNECTION_NEW");
    connAttr.setProperty("NLS_LANG", "ISO-LATIN-1");

    System.out.println("-- Retrieving Connection with New Attributes");

    // Get a Connection from the Oracle DataSource
    OracleConnection conn = (OracleConnection)ods.getConnection(connAttr);

    System.out.println("-- Connection Retrieved. Executing Sample Query in the database");

    // Execute a Sample Query in the database to demonstrate that the
    // connection is established
    executeQuery(conn);

    System.out.println();
    System.out.println("-- Listing Connection Cache Attributes");

    // Print the Connection Attributes onto the console
    printAttributes(conn.getConnectionAttributes());

    // Release the connection back to the MyCache
    conn.close();
  }

  /**
   * This method retrieves the connection with default attributes and then
   * updates it and uses the connection. After its use, the default
   * attributes are again set and the connection is released back to the
   * cache.
   *
   * @exception Exception In Case of an Exception
   */
  private void getConnectionAndUpdateAttributes() throws Exception{
    System.out.println("\n---------- Invoking method "
      + "getConnectionAndUpdateAttributes() ------------------");

    System.out.println("-- Initializing Default Set Attributes");

    // Initialize default Connection Attributes
    java.util.Properties connAttr = new Properties();
    connAttr.setProperty("CONNECTION_NAME", "OTN_CONNECTION");
    connAttr.setProperty("NLS_LANG", "AMERICAN_AMERICA.WE8ISO8859P1");

    System.out.println();
    System.out.println("-- Retrieving Connection with Default Attributes");

    // Get a Connection from the Oracle DataSource
    OracleConnection conn = (OracleConnection)ods.getConnection(connAttr);

    System.out.println("-- Connection Retrieved.");
    System.out.println();

    System.out.println("-- Initializing New Set of Attributes. The new set includes");
    System.out.println("-- TRANSACTION_ISOLATION = SERIALIZABLE");

    // Initialize new Connection Attributes
    Properties newProps = new Properties();
    newProps.setProperty("TRANSACTION_ISOLATION", "SERIALIZABLE");

    System.out.println();
    System.out.println("-- Updating the Connection with new set of Attributes");

    // Update the connection with the new attributes
    conn.applyConnectionAttributes(newProps);

    System.out.println();
    System.out.println("-- Executing Sample Query in the database");

    // Execute a Sample Query in the database to demonstrate that the
    // connection is established
    executeQuery(conn);

    System.out.println();
    System.out.println("-- Listing Connection Cache Attributes");

    // Print the Connection Attributes onto the console
    printAttributes(conn.getConnectionAttributes());
    System.out.println("----> As can be seen, Connection Attributes have been updated");

    System.out.println();
    System.out.println("-- Applying Default Set Attributes again and closing Connection.");

    // Apply the Default Set of attributes to the connection again and
    // release it to MyCache
    conn.close(connAttr);
  }

  /**
   * The 'ClosestConnectionMatch' flag is set in the method
   * setConnectionCacheAttributes(). This means that if there is not an
   * exact attribute match, then retrieve the connection that closely
   * matches.
   *
   * @exception Exception In Case of an Exception
   */
  private void getConnectionWithMatchingAttributes() throws Exception{
    System.out.println("\n-------- Invoking method "
      + "getConnectionWithMatchingAttributes() -----------");

    System.out.println("-- Initializing Matching Set Properties. The matching set includes");
    System.out.println("-- CONNECTION_NAME = OTN_CONNECTION");
    System.out.println("-- NLS_LANG = ENGLSH_UNITED KINGDOM.WE8ISO8859P1");

    // Initialize matching Connection Attributes. Note that the value of the
    // NLS_LANG attribute is different when compared to the default
    // connection attributes.
    java.util.Properties connAttr = new Properties();
    connAttr.setProperty("CONNECTION_NAME", "OTN_CONNECTION");
    connAttr.setProperty("NLS_LANG", "ENGLSH_UNITED KINGDOM.WE8ISO8859P1");

    System.out.println();
    System.out.println("-- Retrieving Connection with matching set of attributes");

    // Get a Connection from the Oracle DataSource
    OracleConnection conn = (OracleConnection)ods.getConnection(connAttr);

    System.out.println();
    System.out.println("-- Connection Retrieved. Executing Sample Query in the database");

    // Execute a Sample Query in the database to demonstrate that the
    // connection is established
    executeQuery(conn);

    System.out.println();
    System.out.println("-- Listing Connection Cache Attributes");

    // Print the Connection Attributes onto the console
    printAttributes(conn.getConnectionAttributes());

    // Release the connection back to the MyCache
    conn.close();
  }

  /**
   * This method strips the connection of all set attributes.
   *
   * @exception Exception In Case of an Exception
   */
  private void clearConnectionAttributes() throws Exception {
    System.out.println("\n------------- Invoking method "
      + "clearConnectionAttributes() -----------");
    System.out.println("-- Initializing Default Set Properties");

    // Initialize default Connection Attributes
    java.util.Properties connAttr = new Properties();
    connAttr.setProperty("CONNECTION_NAME", "OTN_CONNECTION");
    connAttr.setProperty("NLS_LANG", "AMERICAN_AMERICA.WE8ISO8859P1");

    System.out.println("-- Retrieving Connection with Default Set Attributes");

    // Get a Connection from the Oracle DataSource
    OracleConnection conn = (OracleConnection)ods.getConnection(connAttr);

    System.out.println();
    System.out.println("-- Connection Retrieved. Executing Sample Query in the database");

    // Execute a Sample Query in the database to demonstrate that the
    // connection is established
    executeQuery(conn);

    System.out.println("----> Apply an empty set of attributes. This "
      + "strips the connection of all its attributes");

    // The following code clears the connection of all attributes and releases
    // it back to MyCache.
    conn.close(new Properties());
  }

  /**
   * This method prints the attributes onto the console.
   *
   * @param p Properties object holding the attributes as a name-value pair
   */
  private void printAttributes(Properties p){
    // Check if the properties object has some key-value pairs within it
    if (p.elements().hasMoreElements()){
      // It has. Print them onto the console
      p.list(System.out);
    }else{
      // Its empty
      System.out.println("-- NO ATTRIBUTES SET FOR THIS CONNECTION --");
    }
  }

  /**
   * This method executes the query in the database
   *
   * @param conn Connection Object
   * @exception Exception In Case of an Exception
   */
  private void executeQuery(OracleConnection conn) throws Exception{
    // Create a Statement
    Statement stmt = conn.createStatement();

    // Execute the query in the database
    boolean isExecuted = stmt.execute("select count(*) from emp");

    // Test if the query is executed
    if (isExecuted){
      // It has
      System.out.println("Query Executed");
    }else{
      // There is some problem. Check the error on the console
      System.out.println("ERROR : Query not executed");
    }
  }
}

⌨️ 快捷键说明

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