Threadlocal mode for database access [Refer to easydbo]

Source: Internet
Author: User
The jdbccontext class mainly solves data connection problems. Public class jdbccontext {

Private Static final log = logfactory. getlog (jdbccontext. Class );
// Private Database database;
Private datasource;
Protected connection;
Private Boolean isvalid = true;
Private Static threadlocal <jdbccontext> jdbccontext;
/**
* Private constructor
* @ Param Database
*/
Private jdbccontext (datasource ){
This. datasource = datasource;
Createconnection ();
}
Public static jdbccontext getjdbccontext (datasource)
{

If (jdbccontext = NULL) jdbccontext = new jdbccontextthreadlocal (datasource );

Jdbccontext context = (jdbccontext) jdbccontext. Get ();
If (context = NULL ){

Context = new jdbccontext (datasource );

}
Return context;
}

Private void createconnection (){
Try {

If (connection = NULL | connection. isclosed () = true ){
Log. debug ("create a connection ");

// Connection = datasource. getconnection ();
// Isvalid = true;

If (datasource! = NULL ){

// Class. forname (datasource. getdbtype (). getdriverclass (). newinstance ();
// Connection = drivermanager. getconnection (datasource. geturl (), datasource. getuser (), datasource. GetPassword ());
Connection = datasource. getconnection ();
Isvalid = true;

}
Else {
Log. Error ("datasource is invalid ");
Isvalid = false;
Return;
}
} Else {
Log. debug ("the current connection is valid .");
}
} Catch (exception e ){
E. printstacktrace ();
}

}

/**
* Release the current connection;
*/
Public void releaseconnection (){
Try {
If (connection! = NULL & connection. isclosed () = false ){
Connection. Close ();
// Add code by chenying
Jdbccontext. Remove ();
Isvalid = false;
Log. debug ("the connection is closed .");
}
} Catch (sqlexception e ){
E. printstacktrace ();
}
}

/**
* @ Return
*/
Public connection getconnection (){

Log. debug ("get a connection! ");
Try {
If (connection = NULL | connection. isclosed () createconnection ();
}
Catch (exception E)
{
E. printstacktrace ();
}
Return connection;
}
/**
* @ Return
*/
Public Boolean isvalid (){
Return isvalid;
}


/**
* Internal class
* @ Author Administrator
*
*/
Private Static class jdbccontextthreadlocal extends threadlocal <jdbccontext> {
Public datasource;
Public jdbccontextthreadlocal (datasource)
{
This. datasource = datasource;
}
Protected synchronized jdbccontext initialvalue (){
Return new jdbccontext (datasource );
}
}

}

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.