An example of using an Oracle-band connection Buffer class

Source: Internet
Author: User
Tags min

Import java.sql.Connection;
Import Oracle.jdbc.pool.OracleConnectionCacheImpl; /**
* Connection Pool Management class
*/
public class Dbpool {

/** Connection Pool * *
private static Oracleconnectioncacheimpl pool = null;

/**
* Initialize Connection Pool
*/
public static void Init () {

try{
String user = "User"; User name
String password = "pwd"; Password
String host = "127.0.0.1"; Server address
String port = "1521"; Port
String sid = "Oracle92"; Sid
int max = 20; Maximum number of connections
int min = 5; Minimum number of connections

Spelling connection string
String url = "jdbc:oracle:thin:@" + Host + ":" + Port + ":" + sid;

Instantiate a connection pool and set parameters
Pool = new Oracleconnectioncacheimpl ();
Pool.seturl (URL);
Pool.setpassword (password);
Pool.setuser (user);
Pool.setmaxlimit (max);
Pool.setminlimit (min);

/**
* Set the number of connections to increase the mode of exceeding the limit.
* Dynamic_scheme: Dynamic increase. If you exceed the upper limit, turn it off.
* There are two more. 1. return directly null. 2. Always wait
*/
Pool.setcachescheme (Oracleconnectioncacheimpl.dynamic_scheme);

/**
* If you already have a connection pool.
* For example, Oracleconnectionpooldatasource Ocpds;
* Can be set directly. The previous settings are not available. or write directly in the constructor.
*/
Pool.setconnectionpooldatasource (OCPDS);

}catch (Exception ex) {
}

}

/**
* End processing.
* For example, call before app is turned off.
*/
public static void Destory () {
if (pool!= null) {
try {
Pool.close ();
Pool = null;
}catch (Exception ex) {
}
}
}

/**
* Take the connection.
* is as normal as any other connection.
* @return Connection
*/
public static Connection getconnection () {
try{
Connection conn = Pool.getconnection ();
Return conn;
}catch (Exception e) {
return null;
}
}

/**
* Debugging
*/
public static void Turnning () {
System.out.println ("Active Conns:" +pool.getactivesize ());
System.out.println ("Cached Conns:" +pool.getcachesize ());
}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.