[Solved] Avtiviti gets an error when creating a table: Error getting a new connection. Cause: org.apache.commons.dbcp.SQLNestedException

Problem description

An error was reported when using Acitivity to create a ProcessEngineConfiguration object today

The error is as follows

org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Could not create connection to database server.)
### Error getting a new connection. Cause: org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Could not create connection to database server.)
### Cause: org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Could not create connection to database server.)

Analyze the problem

1 There may be a problem with the mysql version driver
2 There may be a configuration error
3 After I tested and inquired about related errors on the Internet, most of the reasons were mysql version problems or configuration errors. For example, the url has parameters, and the driver 8.0 added less cj. I am because of the version problem. My local database is 8.0, but the jar is 5.7

Fix the problem

The relevant code is as follows

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.wyh</groupId>
    <artifactId>Activiti01</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <activiti.version>6.0.0</activiti.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-spring-boot-starter-rest-api</artifactId>
            <version>${<!-- -->activiti.version}</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.18</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

configure

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/contex
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">


    <!--dbcp link pool -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql:///activiti?serverTimezone=UTC"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
        <property name="maxActive" value="3"/>
        <property name="maxIdle" value="1"/>
    </bean>

    <!--In the default mode, the id of the bean is fixed to processEngineConfiguration-->
    <bean id="processEngineConfiguration"
          class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
        <!--Configuration database related information-->
        <!--Database driver
        <property name="jdbcDriver" value="com.mysql.jdbc.Driver"/>-->
        <!--Database link
        <property name="jdbcUrl" value="jdbc:mysql:///activiti"/>-->
        <!--Database username
        <property name="jdbcUsername" value="root"/>-->
        <!--Database password
        <property name="jdbcPassword" value="123456"/>-->
        <!--Directly refer to the link pool configured above-->
        <property name="dataSource" ref="dataSource"/>
        <!--actviti database table generation strategy
        true - if the corresponding table already exists in the database, then use it directly,
               If it does not exist, it will be created -->
        <property name="databaseSchemaUpdate" value="true"/>
    </bean>
</beans>

test class

package com.wyh.test;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.junit.Test;

/**
 * @program: Activiti01
 * @description:
 * @author: Wei Yihe
 * @createDate: 2022-06-22 21:59
 **/

public class TestCreateTable {<!-- -->
    /**
     * Use the default method provided by activiti to create mysql tables
     */
    @Test
    public void testCreateDbTable(){<!-- -->
        // Need to use the tool class ProcessEngines provided by avtiviti, use the method getDefaultProcessEngine
        // getDefaultProcessEngine will read the file named actviti.cfg.xml from resources by default
        // When the processEngine is created, the mysql table will be created
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        System.out.println(processEngine);
    }

}

After the modification, start the test again

You can see that the log information is already creating the table

Check that the database table has been created normally