Two ways to create a database connection pool

Understanding of using DBCP and CP30 to create database connection pool (for college students, please correct if there is something wrong)

Why use this first? First of all, in the early stage, I believe that many small partners will connect to the database in the following ways like me

// The main function is to connect and close the database
public class ConnectionBase
{
	private final String  DbDriver = "com.mysql.jdbc.Driver";//Load the driver. The address of the driver is in quotation marks
	private final String  URL = "jdbc:mysql://localhost:********";//Database connection address
	private final String  user = "****";  //Database name
	private final String  userPWD = "****";	//Database password
	private Connection conn = null ;
	

	public ConnectionBase(String URL){
		try
		{
			Class.forName(DbDriver) ;
			this.conn = DriverManager.getConnection(URL,user,userPWD) ;	
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public ConnectionBase()
	{
		try
		{
			Class.forName(DbDriver) ;
			this.conn = DriverManager.getConnection(URL,user,userPWD) ;	
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	
	
	// Get database connection
	public Connection getConnection()
	{
		return this.conn ;
	}

	
	
	
	// Close database connection
	public void close()
	{
		try
		{
			this.conn.close() ;
		}
		catch (Exception e)
		{
		}		
	}	
};

This is just an example. If you need to create a new Connection in the background in this way, you need to frequently establish a Connection for each visit to the database. Closing the Connection will undoubtedly consume the resources of the server. Then you need to create a Connection pool, not many BB's. let's start:

DBCP creates database connection pool

There are two jar packages needed to create a connection pool using DBCP:
commons-pool-1.5.6.jar
commons-dbcp-1.4.jar

  • Do not use configuration file (generally not used)
public void testDBCP01(){
		
			
			Connection conn = null;
			PreparedStatement ps = null;
			try {
				
				//1. Build data source object
				BasicDataSource dataSource = new BasicDataSource();
				//What kind of database is connected to, which database is accessed, user name, password..
				//jdbc:mysql://localhost/bank master protocol: sub protocol: / / local / database

				//In this part, a large number of set * * methods are used,
				dataSource.setDriverClassName("com.mysql.jdbc.Driver");
				dataSource.setUrl("jdbc:mysql://localhost: port / table name“);
				dataSource.setUsername("root");
				dataSource.setPassword("root");
				
				
				//2. Get connected objects
				conn = dataSource.getConnection();
				String sql = "insert into account values(null , ? , ?)";
				ps = conn.prepareStatement(sql);
				ps.setString(1, "admin");
				ps.setInt(2, 1000);
				
				ps.executeUpdate();
				
			} catch (SQLException e) {
				e.printStackTrace();
			}finally {
				JDBCUtil.release(conn, ps);
			}
			
		}
  • Configuration file: dbcpconfig.properties
  • Here is the contents of dbcpconfig.properties

#connections setting up
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306 / table
username=root
password=root

#
initialSize=10

#Maximum number of connections
maxActive=50

#
maxIdle=20

#
minIdle=5

#
maxWait=60000

#The format of the connection property property attached to the JDBC driver when establishing a connection must be as follows: [property name = property;]
#Note that the "user" and "password" attributes are explicitly passed, so they don't need to be included here.
connectionProperties=useUnicode=true;characterEncoding=gbk

#Specifies the auto commit state of the connection created by the connection pool.
defaultAutoCommit=true

#driver default specifies the transaction level (TransactionIsolation) of the connection created by the connection pool.
#The available values are one of the following: (see javadoc for details.) NONE,READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE
defaultTransactionIsolation=READ_UNCOMMITTED

  • How to use profile (common)
Connection conn = null;
		PreparedStatement ps = null;
		try {
			BasicDataSourceFactory factory = new BasicDataSourceFactory();
			Properties properties = new Properties();
			//Creating database connection pool with file content
			InputStream is = new FileInputStream("src//dbcpconfig.properties");
			properties.load(is);
			DataSource dataSource = factory.createDataSource(properties);
			
			//2. Get connected objects
			conn = dataSource.getConnection();
			String sql = "insert into table values(null , ? , ?)";
			ps = conn.prepareStatement(sql);
			ps.setString(1, "Name");
			ps.setInt(2, 100);
			
			ps.executeUpdate();
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.release(conn, ps);
		}

CP30C create database connection pool

Next time!

Keywords: Database JDBC MySQL SQL

Added by frost110 on Mon, 16 Dec 2019 18:43:09 +0200