Analysis of 2 implementation methods of configuring jnid data source in Tomcatc3p0

Analysis of 2 implementation methods of configuring jnid data source in Tomcatc3p0

Using c3p0

Import the c3p0jar package

<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
  <dependency>
   <groupId>com.mchange</groupId>
   <artifactId>c3p0</artifactId>
   <version>0.9.5.2</version>
  </dependency>

Add data source configuration to the context.xml file of tomcat

<Resource 
    auth="Container" 
    description="DB Connection" 
    driverClass="com.mysql.jdbc.Driver" 
    maxPoolSize="100" minPoolSize="2" 
    acquireIncrement="2" 
    name="jdbc/myDB" 
    user="root" 
    password="123456" 
    factory="org.apache.naming.factory.BeanFactory" 
    type="com.mchange.v2.c3p0.ComboPooledDataSource" 
    jdbcUrl="jdbc:mysql://localhost:3306/attendance_system?characterEncoding=utf8&amp;serverTimezone=GMT%2B8" />

Get connected

protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    try {
      //Create contextContext context=new InitialContext();
      //Get the data source ComboPooledDataSource comboPooledDataSource= (ComboPooledDataSource) context.lookup
          ("java:comp/env/jdbc/myDB");
      //Get database connection Connection connection=comboPooledDataSource.getConnection();
      
      if(!connection.isClosed()){
        System.out.println("Connected successfully");
      }
    } catch (NamingException e) {
      e.printStackTrace();
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }

Using druid

Import jar package

 <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
  <dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>druid</artifactId>
   <version>1.1.16</version>
  </dependency>

Add data source configuration to the context.xml file of tomcat

<Resource
  name="jdbc/MysqlDataSource"
  factory="com.alibaba.druid.pool.DruidDataSourceFactory"
  auth="Container"
  type="javax.sql.DataSource"
  driverClassName="com.mysql.cj.jdbc.Driver"
  url="jdbc:mysql://localhost:3306/yl?characterEncoding=utf8&amp;serverTimezone=GMT%2B8"
  username="root"
  password="123456"
  maxActive="50"
  maxWait="10000"
  removeabandoned="true"
  removeabandonedtimeout="60"
  logabandoned="false"
  filters="stat"/>

Get connected

protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    try {
      //Get the context object Context context=new InitialContext();
      //Get the data source DataSource ds= (DataSource) context.lookup("java:comp/env/jdbc/MysqlDataSource");
      //Get the Connection object Connection connection=ds.getConnection();
​
      if(!connection.isClosed()){
        System.out.println("Connection successful");
​
      }
    } catch (NamingException e) {
      e.printStackTrace();
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Introduction to the principles, configuration, and use of Tomcat data sources
  • Tomcat data source configuration method_JBuilder
  • Three ways to configure JNDI data source in Tomcat

<<:  Sample code using the element calendar component in Vue

>>:  Detailed explanation of the correct use of the count function in MySQL

Recommend

VMware Workstation 14 Pro installs CentOS 7.0

The specific method of installing CentOS 7.0 on V...

Detailed explanation of Linux tee command usage

The tee command is mainly used to output to stand...

A brief summary of my experience in writing HTML pages

It has been three or four months since I joined Wo...

Gallery function implemented by native Js

Table of contents The first The second Native Js ...

Solve the mobile terminal jump problem (CSS transition, target pseudo-class)

Preface Many friends who have just come into cont...

MySQL installation and configuration tutorial for Mac

This article shares the MySQL installation tutori...

How to use negative margin technology to achieve average layout in CSS

We usually use float layout to solve the compatib...

Summary of common docker commands

Docker installation 1. Requirements: Linux kernel...

How to configure tomcat server for eclipse and IDEA

tomcat server configuration When everyone is lear...

How to solve the problem that MySQL cannot start because it cannot create PID

Problem Description The MySQL startup error messa...

How to build mysql master-slave server on centos7 (graphic tutorial)

This article mainly introduces how to build a MyS...

How to periodically clean up images that are None through Jenkins

Preface In the process of continuous code deliver...

MySQL uses variables to implement various sorting

Core code -- Below I will demonstrate the impleme...

Detailed explanation of the use of Arguments object in JavaScript

Table of contents Preface Basic Concepts of Argum...