All about Java usage of oracleconnectionpooldatasource connection pool in the Oracle driver package

Source: Internet
Author: User

Note, the Oracle site found that the method written in this article is no longer in favor of use, see: http://download.oracle.com/docs/cd/B12037_01/java.101/b10979/conncache.htm

A third-party application system uses the sqlserver database, which is managed in a unified manner in the existing system and replaced with the Oracle database as needed. We provide relevant support. The first step is to provide a solution to change to use the Oracle database and include the database connection pool. Third-party applications use custom data structures to store data returned from the database. A simple database connection solution demo is provided for third parties based on the above situation. Demo has three classes in total: pooldbsource // The management connection pool object includes the class for obtaining the connection method. Basemanager // class for database operation encapsulation using the connection obtained by pooldbsource. Connectionpooltestaction
// Struts1.2 action class.

Reference: http://download.oracle.com/docs/cd/A97630_01/java.920/a96654/connpoca.htm

Oracle Database driver package introduced in the demo, which includes the oracleconnectionpooldatasource class that implements the database connection pool.
, You need to go to the Oracle website to download according to your own database version (click to open the link ). The Code is as follows:

Pooldbsource class

Package COM. ***. connectionpool. DB; import Java. SQL. connection; import Java. SQL. sqlexception; import javax. SQL. pooledconnection; import oracle. JDBC. pool. *; public class pooldbsource {Private Static pooldbsource instance; private oracleconnectionpooldatasource ocpds; private pooldbsource () throws sqlexception {ocpds = new oracleconnectionpooldatasource (); ocpps. setdrivertype ("thin"); ocpds. setservername ("202.1" 12.119.177 "); ocpds. setnetworkprotocol ("TCP"); ocpps. setdatabasename ("orcl"); ocpps. setportnumber (1521); ocpds. setuser ("idc_yjs"); ocpds. setpassword ("yjs");}/*** returns a unique instance. if this method is called for the first time, the instance ** @ return pooldbsource unique instance * @ throws sqlexception */static synchronized public pooldbsource getinstance () {If (instance = NULL) will be created) {synchronized (pooldbsource. class) // lock {If (instance = NULL) // The second check {try {Instance = new pooldbsource ();} catch (sqlexception e) {system. out. print ("================================== "); system. out. print ("failed to link to database! "); System. out. print ("================================== "); system. out. print ("error message:"); E. printstacktrace (); return NULL ;}}} return instance;}/*** get database connection * @ return */public connection getconnection () {pooledconnection PC; // database connection pool connection conn; // database connection object try {Pc = octp. getpooledconnection (); // obtain the connection conn = pc from the connection pool. getconnection ();} catch (sqlexception e) {system. out. print ("============================== ================ "); System. Out. Print (" failed to get the database connection! "); System. out. print ("================================== "); system. out. print ("error message:"); E. printstacktrace (); return NULL;} return conn ;}}

Basemanager class

Package COM. ***. connectionpool. common; import Java. SQL. connection; import Java. SQL. resultset; import Java. SQL. sqlexception; import Java. SQL. statement; import COM. ***. connectionpool. DB. pooldbsource; public class basemanager {pooldbsource; // data source connection management object connection conn; // database connection object public basemanager () {super ();} /*** run the query statement to return the dataset * @ Param SQL * @ return */Public resultset executequerysql (string SQL) {statement; resultset rs = NULL; try {pooldbsource = pooldbsource. getinstance (); Conn = pooldbsource. getconnection (); Statement = Conn. createstatement (); RS = statement.exe cutequery (SQL);} catch (sqlexception e) {e. printstacktrace ();} finally {// If (Conn! = NULL) // because they have their own data structure, the actual data structure type returned in this method is not the resultset, // The returned result is a resultset. Therefore, disabling conn here will make the returned Rs unusable. // {// Try // {// Conn. close (); //} // catch (sqlexception e) // {// system. out. print ("an exception occurred when closing the database connection. "); //} Return Rs ;}}

Connectionpooltestaction

Package COM. ***. connectionpool. sources; import Java. SQL. resultset; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; import Org. apache. struts. action. action; import Org. apache. struts. action. actionform; import Org. apache. struts. action. actionforward; import Org. apache. struts. action. actionmapping; import COM. ***. connectionpool. common. basemanager; public class connectionpooltestaction extends action {public actionforward execute (actionmapping mapping, actionform form, httpservletrequest request, response) throws exception {basemanager = new basemanager (); resultset rs = basemanager.exe cutequerysql ("select * From code_school where xsbz = '0'"); If (RS. next () {request. setattribute ("lsh", Rs. getstring (1); Request. setattribute ("zgh", Rs. getstring (2);} Return Mapping. findforward ("success"); // return to a test page to read the request. two setattribute variables }}

If there is any better and more flexible method for the connection pool, please leave a message to me.

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.