"Javaweb" Custom connection pooling Demonstration__web

Source: Internet
Author: User
Tags connection pooling
Package Jdbc.datasource; /** * Customizing a connection Pool * Note: The Create connection pool must implement Java-given DataSource interface, and other vendors must implement the given interface * @author Administrator/import Java.io.Pr
Intwriter;
Import java.sql.Connection;
Import java.sql.SQLException;
Import java.sql.SQLFeatureNotSupportedException;
Import java.util.LinkedList;

Import Java.util.logging.Logger;

Import Javax.sql.DataSource;

Import jdbc.utils.JDBCUtils_V3; public class myDataSource implements datasource{//1. The first step is to create a LinkedList collection for storing the created connection object private static linkedlist<
	Connection> connpool=new linkedlist<> ();
		2. Keep 5 connection objects in the pool static {for (int i=0;i<5;i++) {Connpool.add (jdbcutils_v3.getconnection ()); }/** * Rewrite getconnection () method * * @Override public Connection getconnection () throws SQLException {Connection Co
		Nn=null; 3. Before getting the connection, first determine if there is a connection in the pool of connections if (Connpool.size () ==0) {for (int i=0;i<5;i++) {Connpool.add (Jdbcutils_v3.getconnec
			tion ());
		}//4. Deletes from the connection pool and returns the first connection Conn=connpool.remove (0);
Return conn;	/** * A method of returning a connection created by itself * @param conn/public void Givebackconnection (Connection conn) {CONNPOOL.ADD (conn);
	@Override public PrintWriter Getlogwriter () throws SQLException {return null; @Override public void Setlogwriter (PrintWriter out) throws SQLException {} @Override public void setLoginTimeout
	(int seconds) throws SQLException {} @Override public int getlogintimeout () throws SQLException {return 0;
	@Override public Logger Getparentlogger () throws sqlfeaturenotsupportedexception {return null;
	@Override public <T> T Unwrap (class<t> iface) throws SQLException {return null;
	@Override public boolean iswrapperfor (Class<?> iface) throws SQLException {return false; @Override Public Connection getconnection (string Username, string password) throws SQLException {return null
	;
 }

}

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.