Threadlocal,linkedblockingqueue, thread pool Get database connection 2 improvements

Source: Internet
Author: User
Tags getmessage rand thread class

Package Com.ctl.util;import Java.sql.connection;import Java.sql.drivermanager;import java.sql.sqlexception;import Java.util.random;import Java.util.concurrent.linkedblockingqueue;import Java.util.concurrent.timeunit;import Com.ctl.util.configutils;import Com.ctl.util.logutil;public class Threadlocalblockingqueueutils {private static threadlocal<linkedblockingqueue<connection>> Queuehoder = new threadlocal<linkedblockingqueue< Connection>> ();p ublic static int num = 0;private static string driver;private static string url;private static Strin G username;private static String password;private static int threadpoolmaxnum;private static int threadpoolminnum; private static Logutil log;//\u6570\u636e\u5e93\u7c7b\u578boracle mysql db2private static String databasetype;public static int Getthreadpoolmaxnum () {return threadpoolmaxnum;} public static int Getthreadpoolminnum () {return threadpoolminnum;} public static String GetUrl () {return URL;} public static String GetusernamE () {return username;} public static String GetPassword () {return password;} static {log = new Logutil ();d atabasetype = Configutils.gettype ("DatabaseType"); threadpoolmaxnum = Integer.parseint ( Configutils.gettype ("Threadpoolmaxnum")); threadpoolminnum = Integer.parseint (Configutils.gettype (" Threadpoolminnum ")), if (Databasetype.equals (" MySQL ")) {Driver = Configutils.gettype (" Mysql.driver "); URL = Configutils.gettype ("Mysql.url"), username = Configutils.gettype ("Mysql.username");p Assword = Configutils.gettype (" Mysql.password ");} else if (databasetype.equals ("Oracle")) {Driver = Configutils.gettype ("Oracle.driver"); url = Configutils.gettype (" Oracle.url "), username = Configutils.gettype (" Oracle.username ");p Assword = Configutils.gettype (" Oracle.password ");} try {class.forname (driver);} catch (ClassNotFoundException e) {System.out.println (E.getmessage ());} CreateConnection createconn = new CreateConnection (); Createconn.setdaemon (true); Createconn.start ();} public static synchronized Linkedblockingqueue<Connection> getqueue () {linkedblockingqueue<connection> queue = Queuehoder.get (); if (queue = = null) {queue = new Linkedblockingqueue<connection> (threadpoolmaxnum); Queuehoder.set (queue); return queue;} return queue;} static void Start () {//Tom Tom = new Tom ();//Tom.start ();/for (;;) {//try {//Thread.Sleep ($);//System.out.println ("/*************" +getconnection () + "*************/");/} catch ( Interruptedexception e) {//}//}//}////public static void Main (string[] args) {//Threadlocalblockingqueueutils.start () ;//}public static Connection getconnection () {//System.out.println ("Enter Getconnection"); class Getconnectionclazz Extends Thread {linkedblockingqueue<connection> queue = Threadlocalblockingqueueutils.getqueue ();p rivate Connection conn;private int queuesize;public int getqueuesize () {return queuesize;} Public Connection Getconn () {return conn;} Public synchronized void Run () {//System.out.println ("Enter getconnection run ()"); try {//SYSTEM.ERR.PRINTLN ("-----"+conn+"--------"); while (conn = = null) {//It is very important that there is no such while loop when pressing F5 continuously refreshes, just have a full empty conn = Queue.poll (2, Timeunit.seconds);} Queuesize=queue.size ();//System.err.println ("*******" +conn+ "*********");/if (conn! = null) {//System.err.println ( "\u3010" + queue.size () + "\u3011"//+ "GETCONNECION\U6210\U529F\UFF1A" + conn); \/}} catch (Interruptedexception e) {log. WriteLine ("Getconnection", E.getmessage ());}}} Getconnectionclazz JJ = new Getconnectionclazz (); Jj.start (); try {jj.join ();} catch (Interruptedexception e) {log. WriteLine ("getconnection ()", E.getmessage ());} Log. WriteLine ("getconnection ()", "\u3010" + jj.getqueuesize () + "\u3011" + "GETCONNECION\U6210\U529F\UFF1A" + jj.getconn ()) ; return Jj.getconn ();}} Class GG extends Thread {//public void Run () {//for (;;) {//try {//threadlocalblockingqueueutils.getconnection ();//Thread.Sleep ()//} catch (Exception e) {//}//}//}//}/ * * @descritpion Create a database connection thread class * @author Administrator * */class createconnection extends Thread {Linkedblockingqueue<connection> queue = Threadlocalblockingqueueutils.getqueue (); Logutil log = new Logutil ();p ublic synchronized void Run () {Boolean result = False;while (true) {try {Random rand=new Rand Om (); int num=threadlocalblockingqueueutils.getthreadpoolmaxnum ()- Threadlocalblockingqueueutils.getthreadpoolminnum (); int randsize=rand.nextint (num) +1;if (queue.size () >= Randsize) {thread.sleep (+); continue;} Connection conn = Null;conn = Drivermanager.getconnection (Threadlocalblockingqueueutils.geturl (), Threadlocalblockingqueueutils.getusername (), Threadlocalblockingqueueutils.getpassword ()); if (conn! = null) {result = Queue.offer (conn, 1, timeunit.seconds);} else {//System.out.println ("Drivermanager.getconnection is null"); log. WriteLine ("Createconnection.run ()", "drivermanager.getconnection () returns null"); continue;} if (result = = False) {thread.sleep; log. WriteLine ("Createconnection.run ()", "Maximum number of connections reached queue.size () =" + queue.size ());//System.out.println ("Already full size=" "+ Queue.size () + "" ");} else {log. WriteLine ("Createconnection.run ()", "\u3010" + queue.size () + "\u3011" + "createconnection success:" + conn);// System.out.println ("\u3010" + queue.size () + "\u3011"//+ "CreateConnection success:" + conn);}} catch (Interruptedexception e) {//E.printstacktrace (); log. WriteLine ("Getconnection", E.getmessage ());//System.err.println (E.getmessage ());} catch (SQLException e) {log. WriteLine ("Getconnection", E.getmessage ());//E.printstacktrace ();//System.err.println (E.getmessage ());}}}

Threadlocal,linkedblockingqueue, thread pool Get database connection 2 improvements

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.