PHP笔记网

革命尚未成功,同志仍须努力下载JDK17

作者:Albert.Wen  添加时间:2021-04-20 16:54:55  修改时间:2024-11-12 12:01:49  分类:06.Java框架/系统  编辑

最近搭建了一个Spring MVC项目,配置完数据库连接,运行时一直报错:

警告: com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@1316264 -- Acquisition Attempt Failed!!! Clearing pending acquires. While trying to acquire a needed new resource, we failed to succeed more than the maximum number of allowed acquisition attempts (30). Last acquisition attempt exception: com.microsoft.sqlserver.jdbc.SQLServerException: 用户 'wenjianbao' 登录失败。
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:196)
at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:246)
at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:83)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:2532)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:1929)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:41)
at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:1917)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4026)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1416)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1061)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:833)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:716)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:841)
at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:134)

数据库配置如下:

(1) 文件 spring-mybatis.xml

<context:property-placeholder location="classpath:/db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"
      p:driverClass="${driver}"
      p:jdbcUrl="${url}"
      p:user="${username}"
      p:password="${password}"
      p:idleConnectionTestPeriod="${idleConnectionTestPeriod}"
      p:maxIdleTime="${maxIdleTime}"
      p:acquireIncrement="${acquireIncrement}"
      p:initialPoolSize="${initialPoolSize}"
      p:maxPoolSize="${maxPoolSize}"
      p:minPoolSize="${minPoolSize}"
      p:autoCommitOnClose="${autoCommitOnClose}"
      p:checkoutTimeout="${checkoutTimeout}"
      p:acquireRetryAttempts="${acquireRetryAttempts}"
      p:preferredTestQuery="SELECT 1"
      p:maxConnectionAge="3000"/>

(2) 文件 db.properties

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/java_web?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false
username=root
password=123456

idleConnectionTestPeriod=60
maxIdleTime=240
acquireIncrement=5
initialPoolSize=10
maxPoolSize=30
minPoolSize=10
autoCommitOnClose=false
checkoutTimeout=1000
acquireRetryAttempts=2

 

检查了半天,读取配置没问题,数据库配置也没问题。最后看到一篇博客说是${}取值的问题,觉得可能是这个问题,于是把数据库配置改成如下:

(1) 文件 spring-mybatis.xml

<context:property-placeholder location="classpath:/db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"
      p:driverClass="${db_driver}"
      p:jdbcUrl="${db_url}"
      p:user="${db_username}"
      p:password="${db_password}"
      p:idleConnectionTestPeriod="${db_idleConnectionTestPeriod}"
      p:maxIdleTime="${db_maxIdleTime}"
      p:acquireIncrement="${db_acquireIncrement}"
      p:initialPoolSize="${db_initialPoolSize}"
      p:maxPoolSize="${db_maxPoolSize}"
      p:minPoolSize="${db_minPoolSize}"
      p:autoCommitOnClose="${db_autoCommitOnClose}"
      p:checkoutTimeout="${db_checkoutTimeout}"
      p:acquireRetryAttempts="${db_acquireRetryAttempts}"
      p:preferredTestQuery="SELECT 1"
      p:maxConnectionAge="3000"/>

(2) 文件 db.properties

db_driver=com.mysql.cj.jdbc.Driver
db_url=jdbc:mysql://localhost:3306/java_web?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false
db_username=root
db_password=123456

db_idleConnectionTestPeriod=60
db_maxIdleTime=240
db_acquireIncrement=5
db_initialPoolSize=10
db_maxPoolSize=30
db_minPoolSize=10
db_autoCommitOnClose=false
db_checkoutTimeout=1000
db_acquireRetryAttempts=2

然后,数据连接成功了。

问题原因:

变量名有冲突,重名了,加个前置(如:db_)保证其唯一性 即可!

 

参考:https://blog.csdn.net/qq_21134557/article/details/81004205

 

延伸阅读:

配置文件中propertyConfigurer类的使用

Spring中context:property-placeholder标签的使用说明