Data Source (connection pool), data source connection

Source: Internet
Author: User

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 ();

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.