Connection- using tomcat connection pool to establish connections Connecton

Copyright: [Beijing] Java Youth: 456588754 https://blog.csdn.net/Amen_Wu/article/details/53384987

Previously created connection pool N connections, facilitate multi-user database connection.
This article tomcat connection pooling because the bound address information database, user, password, etc., and need support to start tomcat, is not convenient in the test project, modify the database connection and so on.

step:

  1. Add the following configuration to configure tomcat context.xml
<Resourcename="jdbc/news"auth="Container" type="javax.sql.DataSource"maxActive="100" 
maxIdle="30"
 maxWait="10000" username="scott" password="tiger"
		driverClassName="oracle.jdbc.driver.OracleDriver" url="jdbc:oracle:thin:@localhost:1521:ORCL" />

Parameters:
name indicates the name of the specified jndi (can easily take themselves)
the auth represents authentication mode, generally Container
data indicating the type of source type, using standard the javax.sql.DataSource
for maxActive represents one of the largest connection pool database connections. Set to 0 for unlimited.
maxIdle indicates the maximum number of idle connections, idle time exceeds, the database connection is marked as unavailable, and then released. Set to 0 for unlimited.
maxWait When the database connection pool already occupied, the maximum waiting time
username represents the database user name
password represents the database user's password
driverClassName DRIVER represent JDBC
url represents the database URL address

2. The code is written in jsp or general class (the need to ensure tomcat start), rather than the main method because the pool connections are provided by tomcat
run the main method does not need to start tomcat

  <%
 Context cxt=new InitialContext();
 DataSource ds=(DataSource) cxt.lookup("java:comp/env/jdbc/news");
 Connection conn= ds.getConnection();
 out.print("conn: "+conn);
%>

Guess you like

Origin blog.csdn.net/Amen_Wu/article/details/53384987