Data Source (connection pool), data source connection
Data sources are generally implemented from javax. SQL. dataSource interface, Spring, Struts, Hibernate and other frameworks all have their own data sources. Tomcat also has built-in data source support. Tomcat uses Jakarta-CommonsDatabase Connection Pool as the data source implementation, you only need to configure it according to the Tomcat document.
The data source can be configured in server. xml or context. xml.
Configure the data source in context. xml
<Context cookies="true"> <Resource name="jdbc/databaseWeb" auth = "Container" type = "javax.sqlDataSource" maxActive = "100" maxIdle = "30" maxWait = "10000" username = "root" password = "admin" driverClassName = "com.mysql.jdbc.Diver" url = "jdbc:mysql://localhost:3306/databaseWeb?characterEncoding=utf8"></Context>
Note that the Mysql driver should be placed in the global lib of Tomcat.
Configure the data source application in the web. xml of the application:
<resource-ref> <description>DBConnection</description> <res-ref-name>jdbc/databaseWeb</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth></resource-ref>
Use a data source in a java file:
Context initContext = new InitialContext (); // obtain all resources. Context envContext = initContext. lookup ("java:/comp/env"); obtain the JNDI resource DataSource ds = (DataSource) envContext. lookup ("jdbc/databaseWeb"); // obtain the database Connection conn = ds. getConnection ();