[Spring] Spring JDBC连接不上的问题解决

在学习Spring JDBC的过程中,遇到如下问题。描述如下:

可以连接到 test 数据库和information_schema数据库(test和information_schema数据库为MySQL安装后本身就有的数据库),但是连接不上自己create的数据库。测试过程显示错误信息为:

警告: com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@4b76c591 -- 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: 
java.sql.SQLException: Access denied for user 'arthur'@'localhost' (using password: YES)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3515)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3447)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:911)
	at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3953)
	at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1276)
	at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2048)
	at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:723)
	at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
	at sun.reflect.GeneratedConstructorAccessor7.newInstance(Unknown Source)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
	at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
	at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
	at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)
	at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:134)
	at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:182)
	at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:171)
	at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:137)
	at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1014)
	at com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:32)
	at com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1810)
	at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)

可以发现,该错误产生原因:账号密码的问题!但是注意!!!可以连接上 test数据库。

后来笔者经过测试,发现,只要密码为空,任意字符串的账号都可以访问test数据库,笔者认为这是MySQL不好的一个地方,仅供参考(因为我不能理解test数据库这样设置的缘由)。

笔者连接数据库采用的是c3p0连接池的方式,这里有个致命的错误。如下:

这是配置c3p0的bean。

<!-- 配置c3p0数据源 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="user" value="${user}"></property>
		<property name="password" value="${password}"></property> 
		<property name="jdbcUrl" value="${JdbcUrl}"></property>
		<property name="driverClass" value="${driverClass}"></property>
		<property name="numHelperThreads" value="10"></property>
			<property name="initialPoolSize" value="${initPoolSize}"></property>
		<property name="maxPoolSize" value="${maxPoolSize}"></property>
	</bean>


致命错误在于
<property name="user" value="${user}"></property>
value="${user}",注意!!!这里不能用user!!!改成

<property name="user" value="${username}"></property>
问题解决,成功连接。


综上,该问题产生的原因是:变量名称可能会与系统或者框架中的变量产生冲突。所以在命名时,不要采用可能会冲突的名称。



你可能感兴趣的:([Spring] Spring JDBC连接不上的问题解决)