/**
* occipool - Demontrating the Connection Pool interface of .
*
* DESCRIPTION :
* This program demonstates the creating and using of connection pool in the
* database and fetching records of a table.
*
* Make sure that the setup file, occidemo.sql is run prior to
* running this program.
*
*/
#include <iostream>
#include <occi.h>
using namespace oracle::occi;
using namespace std;
class occipool
{
private:
Environment *env;
Connection *con;
Statement *stmt;
public :
/**
* Constructor for the occipool test case.
*/
occipool ()
{
env = Environment::createEnvironment (Environment::DEFAULT);
}// end of constructor occipool ()
/**
* Destructor for the occipool test case.
*/
~occipool ()
{
Environment::terminateEnvironment (env);
} // end of ~occipool ()
/**
* The testing logic of the test case.
*/
dvoid select ()
{
cout << "occipool - Selecting records using ConnectionPool interface" <<
endl;
const string poolUserName = "hr";
const string poolPassword = "siporhr";
const string connectString = "(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.75)(PORT = 1521)))(CONNECT_DATA = (SERVER = DEDICATED)(SERVICE_NAME = sipor10g)))";
const string username = "hr";
const string passWord = "siporhr";
unsigned int maxConn = 5;
unsigned int minConn = 3;
unsigned int incrConn = 2;
ConnectionPool *connPool;
try{
/*
创建连接池
ConnectionPool* createConnectionPool(const string &poolUserName,
const string &poolPassword,
const string &connectString = "",
unsigned int minConn = 0,
unsigned int maxConn = 1,
unsigned int incrConn = 1);
Environment Class's function
This method creates a connection pool based on the parameters specified.
参数
poolUserName
The pool user name.
poolPassword
The pool password.
connectString
The database to connect to.
minConn
The minimum number of connections in the pool. The minimum number of connections are opened by this method. Additional connections are opened only
when necessary. Generally, minConn should be set to the number of concurrent statements the application is expected to run.
maxConn
The maximum number of connections in the pool.
Valid values are 1 and greater.
incrConn
The increment by which to increase the number of connections to be opened if the current number of connections is less than connMax.
Valid values are 1 and greater.
*/
connPool = env->createConnectionPool
(poolUserName, poolPassword, connectString, minConn, maxConn, incrConn);
if (connPool)
cout << "SUCCESS - createConnectionPool" << endl;
else
cout << "FAILURE - createConnectionPool" << endl;
/*
从连接池中取一个连接
Connection* createConnection(const string &userName,
const string &password);
ConnectionPool Class's function
This method creates a pooled connection.
参数
userName
The name of the user to connect as.
password
The password of the user.
*/
con = connPool->createConnection (username, passWord);
if (con)
cout << "SUCCESS - createConnection" << endl;
else
cout << "FAILURE - createConnection" << endl;
}catch(SQLException ex)
{
cout<<"Exception thrown for createConnectionPool"<<endl;
cout<<"Error number: "<< ex.getErrorCode() << endl;
cout<<ex.getMessage() << endl;
return;
}
cout << "retrieving the data" << endl;
try{
/*
创建sql指定的SQL语句
Statement* createStatement(const string &sql ="");
Connection Class's function
This method creates a Statement object with the SQL statement specified.
参数
sql
The SQL string to be associated with the statement object.
*/
stmt = con->createStatement
("SELECT author_id, author_name FROM author_tab");
/*
执行sql指定的SQL语句
ResultSet * executeQuery(const string &sql = "");
Statement Class's function
Execute a SQL statement that returns a ResultSet. Should not be called for a statement which is not a query, has streamed parameters.
Returns a ResultSet that contains the data produced by the query
参数
sql
sql statement to be executed. This can be null if setSQL() was used to associate the sql with the statement.
*/
ResultSet *rset = stmt->executeQuery();
while (rset->next())
{
cout << "author_id:" << rset->getInt (1) << endl;
cout << "author_name:" << rset->getString (2) << endl;
}
//释放一个rset
//In many cases, it is desirable to immediately release a result set's database and OCCI resources instead of waiting for this to happen
//when it is automatically closed; the closeResultSet method provides this immediate release.
stmt->closeResultSet (rset);
//关闭一个stmt
//This method closes a Statement object and frees all resources associated with it.
con->terminateStatement (stmt);
//关闭一个连接con
connPool->terminateConnection (con);
//终止连接池connPool
env->terminateConnectionPool (connPool);
}catch(SQLException ex)
{
cout<<"Exception thrown for retrieving data"<<endl;
cout<<"Error number: "<< ex.getErrorCode() << endl;
cout<<ex.getMessage() << endl;
}
cout << "occipool - done" << endl;
} // end of test (Connection *)
}; // end of class occipool
int main (void)
{
string user = "HR";
string passwd = "HR";
string db = "";
cout << "occipool - Exhibiting interoperability of and OCI" << endl;
occipool *demo = new occipool ();
demo->select();
delete demo;
}// end of main ()