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

How to optimize the slow Like fuzzy query in MySQL

Table of contents 1. Introduction: 2. The first i...

Linux system prohibits remote login command of root account

ps: Here is how to disable remote login of root a...

Detailed example of mysql trigger usage

MySQL trigger syntax details: A trigger is a spec...

MySQL subqueries and grouped queries

Table of contents Overview Subqueries Subquery Cl...

Usage instructions for the docker create command

The docker create command can create a container ...

Tutorial on compiling and installing MySQL 5.7.17 from source code on Mac

1. Download and unzip to: /Users/xiechunping/Soft...

Change the MySQL database engine to InnoDB

PS: I use PHPStudy2016 here 1. Stop MySQL during ...

MySQL Series 4 SQL Syntax

Table of contents Tutorial Series 1. Introduction...

Native js implements shopping cart logic and functions

This article example shares the specific code of ...

Detailed analysis of MySQL master-slave replication

Preface: In MySQL, the master-slave architecture ...

What does mysql database do?

MySQL is a relational database management system....

Analysis of MySQL latency issues and data flushing strategy process

Table of contents 1. MySQL replication process 2....

A Brief Analysis of Patroni in Docker Containers

Table of contents Create an image File Structure ...