Avtiviti创建表时报错:Error getting a new connection. Cause: org.apache.commons.dbcp.SQLNestedException
Posted 梦与光同行
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Avtiviti创建表时报错:Error getting a new connection. Cause: org.apache.commons.dbcp.SQLNestedException相关的知识,希望对你有一定的参考价值。
问题描述
今天使用Acitivity创建ProcessEngineConfiguration对象时报错
错误如下
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.)
分析问题
1 有可能是mysql版本驱动的问题
2 有可能是配置错误问题
3 经我测试和查询网上的相关错误,大部分原因都是mysql版本问题或者配置错误,比如url确实参数,驱动8.0少加了cj,我是因为版本问题,我本地是8.0的数据库,但是jar是5.7的
解决问题
相关代码如下
<?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>
配置
<?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链接池-->
<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>
<!--在默认方式下 bean的id 固定为 processEngineConfiguration-->
<bean id="processEngineConfiguration"
class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
<!--配置数据库相关的信息-->
<!--数据库驱动
<property name="jdbcDriver" value="com.mysql.jdbc.Driver"/>-->
<!--数据库链接
<property name="jdbcUrl" value="jdbc:mysql:///activiti"/>-->
<!--数据库用户名
<property name="jdbcUsername" value="root"/>-->
<!--数据库密码
<property name="jdbcPassword" value="123456"/>-->
<!--直接引用上面配置的链接池-->
<property name="dataSource" ref="dataSource"/>
<!--actviti数据库表在生成时的策略
true - 如果数据库中已经存在相应的表,那么直接使用,
如果不存在,那么会创建-->
<property name="databaseSchemaUpdate" value="true"/>
</bean>
</beans>
测试类
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: 魏一鹤
* @createDate: 2022-06-22 21:59
**/
public class TestCreateTable
/**
* 使用activiti提供的默认方式来创建mysql的表
*/
@Test
public void testCreateDbTable()
// 需要使用avtiviti提供的工具类 ProcessEngines ,使用方法getDefaultProcessEngine
// getDefaultProcessEngine会默认从resources下读取名字为actviti.cfg.xml的文件
// 创建processEngine时,就会创建mysql的表
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
System.out.println(processEngine);
修改完之后再次启动测试
可以看到日志信息已经在创建表了
查看数据库 表已经正常创建
以上是关于Avtiviti创建表时报错:Error getting a new connection. Cause: org.apache.commons.dbcp.SQLNestedException的主要内容,如果未能解决你的问题,请参考以下文章
报错集:给新创建的MySQL数据库设置用户密码时报错(error: ‘Access denied for user ‘root‘@‘localhost‘ (using password: NO))