Simple pool implementation

Source: Internet
Author: User

The main function of this component is to process a small number of instances and implement the pooling function. A preparedstatement pool is added on the basis of this component. As long as the classes using this pool are the same as the implementation classes, write the two methods into the package org. koron. db. exchange; import java. SQL. *; import org. koron. db. DBConnection; public class SwanStatementPool extends SwanPool <PreparedStatement> {private String alias; private String SQL; public SwanStatementPool (String alias, String SQL) {super (); this. alias = alias; this. SQL = SQL;} public String getAlias () {return alias;} public void SetAlias (String alias) {this. alias = alias;} public String getSql () {return SQL;} public void setSql (String SQL) {this. SQL = SQL ;}@ Overridepublic PreparedStatement add () {try {return DBConnection. getConnection (alias ). prepareStatement (SQL);} catch (SQLException e) {e. printStackTrace () ;}return null ;}@ Overridepublic boolean remove (PreparedStatement t) {try {if (t! = Null) {Connection connection = t. getConnection (); if (! T. isClosed () t. close (); connection. close () ;}} catch (SQLException e) {e. printStackTrace () ;}return false ;}} ==================================================== abstract class =============== package org. koron. db. exchange; import java. util. arrayList;/*** implement the pool function **. This class of inheritance classes only need to implement the add and remove methods. * The add method is to generate instances in a pool, and remove is to release instances in a pool. * @ Author FANGZW **/public abstract class SwanPool <T> {/*** maximum pool */private int maxSize = 5; /***** min count */private int minSize = 1;/***** Max idle count */private int idleSize = 1;/***** Max wait time, if the time limit is exceeded, forcibly recycle it. Call the remove Method */private int maxPeriod;/***. When the resource cannot be obtained, subsequent requests will enter the wait queue. If the queue length is exceeded, the return value is null *-1, indicating no limit * this variable is not used for the moment */private int buffers =-1;/*** indicates the maximum wait time of the resource application thread. Unit: millisecond */private int timeout = 5000;/*** resource pool */private ArrayList <PoolData Unit <T> al = new ArrayList <PoolDataUnit <T> (); class PoolDataUnit <T> {/*** borrowed resources */private T; /* ** lending time */private long borrowTime;/* indicates whether or not the database has been lent */private boolean borrowed; public PoolDataUnit (T t) {super (); this. t = t; this. borrowTime =-1; this. borrowed = false;} public T getT () {return t;} public void setT (T t) {this. t = t;} public long getBorrowTime () {return borrowTime;} public void setBorrowTime (long borr OwTime) {this. borrowTime = borrowTime;} public boolean isBorrowed () {return borrowed;} public void setBorrowed (boolean borrowed) {this. borrowed = borrowed;}/*** generated resources are used in {@ link # borrow. When all resources are used, call this method and add this object to the pool. * @ Return */public abstract T add ();/*** reclaim Resources * @ param t * @ return */public abstract boolean remove (T t ); /*** call {@ link # nativeBorrow ()} to obtain the resource. * If there are no resources in the pool, put some objects in the waiting queue and wait for the notification * @ return */public synchronized T borrow () {long enterStamp = System. currentTimeMillis (); T t = nativeBorrow (); try {while (t = null & System. currentTimeMillis ()-enterStamp <timeout) // wait for {wait (); t = nativeBorrow ();} return t;} catch (InterruptedException e) {e. printStackTrace ();} return null;}/*** internal return resource. If a resource is not used, return directly. * If there are still Resources in the pool that have been created, return directly and set the status. * If there are no idle resources and the number of resources is smaller than the maximum number of resources, call {@ link # add ()} to generate the resource, and set the status to return this resource * @ return */private synchronized T nativeBorrow () {for (PoolDataUnit <T> unit: al) {if (System. currentTimeMillis ()-unit. getBorrowTime ()> maxPeriod) // If the resource has expired, call the remove method to remove the resource (unit. getT (); if (! Unit. isBorrowed () // if this resource is not used or the time used by this resource exceeds the time limit {unit. setBorrowed (true); unit. setBorrowTime (System. currentTimeMillis (); return unit. getT () ;}} if (al. size () <maxSize) {T t = add (); PoolDataUnit <T> unit = new PoolDataUnit <T> (t); al. add (unit); unit. setBorrowed (true); unit. setBorrowTime (System. currentTimeMillis (); return t;} return null;}/*** return the object and put it back into the pool, all threads * @ param t */public synchronized void lent (T t) {for (PoolDataUnit <T> unit: al) {if (unit. getT (). equals (t) {unit. setBorrowed (false); unit. setBorrowTime (System. currentTimeMillis (); policyall (); return ;}} remove (t );}}

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.