the data source in Java is Javax.sql.DataSource. The creation of DataSource can have different implementations. DataSource is often referred to as a data source, which contains two parts of connection pooling and connection pooling management, often referred to DataSource as a connection pool.
jndi mode creates DataSource: (Java naming and directory Interface,java naming and directory interfaces). First, you configure the connection information for the data source, which is the data source connection pool. This configuration should be configured in the Conf/context.xml file in the Tomcat installation directory .
Connection Pool basic idea, principle:
When the system is initialized, the database connection is stored as an object in memory, and when the user needs to access the database, instead of establishing a new connection, an established idle connection object is fetched from the connection pool.
The database connection pool is responsible for allocating , managing, and freeing the database connection , which allows the application to reuse an existing database connection instead of re-establishing one.
Connection Pooling Popular Understanding:
Database connection pooling is to prepare a pool, which contains a lot of generated good Connection, the user requests to get a connection, you do not need to getconnection, just take one from the pool to him, This eliminates the time to generate Connection , the efficiency will be greatly improved, but of course, will occupy some memory ~ slightly larger sites will use the database connection pool ~
Benefits of database connection pooling technology:
Resource Reuse
Faster system response Speed
New means of resource allocation
Unified connection management to avoid database connection leaks
Open source database connection pool c3p0 DBCP Proxool Xapool etc.
Take C3P0 to get the data source as an example:
@Test
public void Testc3p0 () throws exception{ combopooleddatasource CPDs = new Combopooleddatasource (); Cpds.setdriverclass ("Com.mysql.jdbc.Driver"); Loads the JDBC driver cpds.setjdbcurl ("Jdbc:mysql:///atguigu"); Cpds.setuser ("root"); Cpds.setpassword ("1230"); System.out.println (Cpds.getconnection ()); }
Data source and database connection pool