Java Development Notes usage (one hundred fifty) C3P0 connection pool

JDBC both the development of uniform standards compatible with multiple databases, and report the use of pre-blocked by SQL injection vulnerability has been very fair to say perfect, but the unexpected outbreak, it is not satisfactory in terms of performance. The problem is in the management of database connections, in accordance with normal procedure, each complete operation of the database, we must close the connection, whether the code manually closed or automatically closed by a try statement. If you did not close the database connection, the database will be prolonged occupation of limited memory, resulting in unnecessary waste of system resources. However, frequent switching database connection is also faulty, because each get operation to be processed by the CPU, often connecting to the database will increase the burden on the CPU. Memory and CPU seems like a pair of fellow sufferers, no matter how it does affect one of them, the so-called fish and can not have both.
In fact, similar cases connected with the thread, the thread is also a headache frequently lead to resource overhead created, for which early on Java thread pool mechanism is designed in advance to accommodate several threads in a pond, you need to use threads will pick from a thread of execution task, the task done and then return the thread, so to achieve the recycling of resources thread, effectively improving the overall efficiency of the system. Since the thread pool threads have set up a big family, so they can be connected to the composition of the family connection pool it? Although the Java tool comes with a thread pool, but failed to launch a similar connection pooling tools, so a variety of third-party connection pool swarming into the sky, such as DBCP, C3P0, Proxool and so on, which is widely used when the number of C3P0.
C3P0 is an open source database connection pool, it supports JDBC3 norms and standards JDBC2 extension. To use the Java project C3P0, it must first import the jar package, such as c3p0-0.9.5.4.jar, but also to import the jar package depends mchange-commons-java-0.2.16.jar, that is, a total of import two jar files. C3P0 is very simple to use and master the use of ComboPooledDataSource class is enough common methods of the class as follows:
setDriverClass: database-driven set the connection pool.
setJdbcUrl: set the connection address database.
setUser: Set the user name of the database.
setPassword: password database.
setMaxPoolSize: set the upper connection pool size.
setMinPoolSize: setting a lower limit of the size of the connection pool.
setInitialPoolSize: set the initial size of the connection pool.
setMaxStatements: set the maximum number of reports.
setCheckoutTimeout: setting acquisition latency in milliseconds connected. When all the connection pool are occupied, the new request would like to get a connection must wait, wait for existing connections in order to obtain an idle connection after being released. The default of 0 means wait forever.
setMaxIdleTime: set the maximum idle time in seconds. If a connection is not used this time is exceeded, it will be automatically recovered. The default is 0 means no judge time-out, which is never recovered.
getConnection: obtaining a connection from the connection pool.
close: close the connection pool.
After the introduction of the connection pool, complete database operation process is decomposed into two steps: initialization connection pool, taken from a connection pool connection processing, following description separately.
1, initialize the connection pool
object is created the first step C3P0 connection pool, then call related methods in order to set detailed parameters, including database-driven, connection address, user name, password, and related to the connection pool specifications. The following is an example of code initializes C3P0 connection pool:

	private static ComboPooledDataSource dataSource; // declare objects C3P0 connection pool 
	// initialize the connection pool 
	Private initDataSource static void () { 
		the dataSource = new new ComboPooledDataSource (); // Create a connection pool C3P0 
		the try { 
			dataSource.setDriverClass (driver_class); // Set a database connection pool driving 
		} the catch (PropertyVetoException E) { 
			e.printStackTrace (); 
		} 
		dataSource.setJdbcUrl (dbUrl); // set the connection address database 
		dataSource.setUser (dbUserName); // set the user name of the database 
		dataSource.setPassword (dbPassword); // set the password database 
		dataSource.setMaxPoolSize (10); // set the maximum size of the connection pool 
		dataSource.setMinPoolSize (1); // set the lower limit of the size of the connection pool 
		dataSource.setInitialPoolSize (3); // set the initial size of the connection pool 
	}

  

2, taken from a connection pool connection processing
after the start in addition to a call connection pool getConnection get a connection outside the process steps consistent with the operation of the remaining original JDBC process, i.e., a database connection is obtained, similarly connected to create a report, then ordered report on the implementation of SQL statements. The following is the code through the operation of the database connection pool examples:

	// display gender packet 
	Private static void showRecordGroupBySex () { 
		String = SQL "SELECT Sex, COUNT (. 1) by COUNT from Teacher Group Sex Sex by ASC Order"; 
		// get a connection from the connection pool, to create a report of the connection, the command performing the specified SQL statement reporting 
		the try (Conn = dataSource.getConnection Connection (); 
				the statement stmt = conn.createStatement (); 
				the ResultSet stmt.executeQuery RS = (SQL)) { 
			the while (rs.next ()) {// loop through all records of the result set which 
				int sex = rs.getInt ( "sex" ); // Get the specified field integer value 
				int count = rs.getInt ( "count" ); // Get the integer value of the specified field 
				String desc = String.format ( "% s% d teacher has bits;", sex == 0 "M":? "female", COUNT); 
				of System.out.print (desc); 
			} 
		} the catch (SQLException E) { 
			E .printStackTrace ();
		}
	}

 

Code initializes a specific operation and integration of the connection pool, run a test program integrated within the code, were observed following log understood C3P0 connection pool work.

There are two male teacher; there are three female teachers;  



See more Java technology articles " Java Development Notes (order) List of chapters "

Guess you like

Origin www.cnblogs.com/pinlantu/p/11525758.html