Tomcat 7 dbcp Configuration database connection pool detail

  • 2020-06-12 11:28:31
  • OfStack

Tomcat 7-ES2en Configuration database connection pool detail

The principle of

Everyone knows about connection pooling, which is used to qualify connections to databases. The basic principle is to put 1 fixed free connection in the buffer pool in advance. When the program needs to interact with the database, instead of creating a new database connection, it will directly fetch from the connection pool and return to the connection pool after use. Why sacrifice a buffer to hold connections that are already in use? One of the advantages mentioned above is that you can limit the number of connections, which will not cause the database connection of N to crash. With an additional connection pool like this, you can also listen on these connections and manage them easily.

configuration

1. Copy the relevant jar

Remember that connection pooling is not used to manipulate the database directly, and it is the associated jdbc driver that ultimately does so. If it is an tomcat server, copy the driver directly to tomncat's lib. For Java it is ojdbc6.ES21en; For sqlserver, it is the driver of ES23en-ES24en.jar, ES26en-ES27en.jar and sql server. sqljdbc4.jar package to the web-ES34en folder of the project file.

2. Configuration context. xml


   name="jdbc/drp" 
   auth="Container" 
   type="javax.sql.DataSource" 
   factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" 
   maxActive="100" 
   maxIdle="30" 
 maxWait="10000" 
 username="drp1" 
 password="drp1" 
 driverClassName="oracle.jdbc.driver.OracleDriver" 
 url="jdbc:oracle:thin:@localhost:1521:drp" /> 

For global configuration, place the ES44en.xml file in the tomcat configuration folder, conf. If you want local control, you need to place ES48en.xml in the meta-inf of webroot for the specified project, and it will only work for your own project.

Note:


Name: Specifies the name of the connection pool 
Auth: Is the connection pool management rights property, Container Presentation container management 
type : Data source type 
factory : This is in tomcat 5 After the commons-dbcp the 1 The treatment scheme is specifically used like this 1 Is handled by a specified implementation class. Better performance and compatibility dbcp . 
maxActive : Maximum number of allocated connections. 
maxIdle : idle It means free, so this is when tomcat At startup, the buffer pool is the number of new connections to the connection. 
maxWait : The maximum response time for this article is 10s . 
url : is the database address. 
sqlserver The format of: jdbc:sqlserver://localhost:1433;DatabaseName=name ; 
driverclassname : Drive address. 
sqlserver To: com.microsoft.sqlserver.jdbc.SQLServerDriver

3. Verify


Connectionconn=null; 
PreparedStatementpstmt=null; 
ResultSetrs=null; 
try{ 
// The instance DBCP The connection pool  
Contextctx=new InitialContext(); 
// through JNDI Access the specified connection pool  
DataSourceds=(DataSource)ctx.lookup("java:comp/env/jdbc/drp"); 
// Instantiate a database connection  
conn=ds.getConnection(); 
// The query  
pstmt=conn.prepareStatement("select* from t_user"); 
rs=pstmt.executeQuery(); 
if(rs.next()){ 
System.out.print(rs.getString("user_id")+rs.getString("user_name")); 
System.out.print(conn); 
} 
}catch(SQLExceptione){ 
 
} 
finally{ 
 
} 

After the order

Previous versions of tomcat 5 can be view-configured in localhost:8080/admin or xml. The view-based configuration process is then deactivated. In general, for the data connection processing scheme, it has its advantages, but also requires developers to develop good habits in the ordinary programming, if the open connection is not closed, when the connection pool is large enough, it will affect performance; If it peaks, the program simply crashes. It also reduces the time we spend creating database connections. Of course, in addition to dbcp, there are also C3P0, Poolmen such processing scheme.

Thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: