原文鏈接 :http://blog.csdn.net/lindonglian/article/details/48001441
早上在做MyBatis+spring整合的時候爆了個奇葩的bug,
遇到這個問題換一個讀取配置文件方法即可,測試成功!
之前用同一個MySQL都是可以的,現在password=YES !!
百度雖然很強大,找個解決方案還真不容易,應該很少人會出現同樣的。。
既然問題出現在數據庫連接上,那就應該找數據庫連接相關的配置。
最后問題的解決在配置文件導入上,我這里是
<context:property-placeholder location="classpath:db.properties" />
有網友是這樣導入的
- <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <list>
- <value>classpath*:db.properties</value>
- </list>
- </property>
- </bean>
- rg.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
- ### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Access denied for user 'Administrator'@'localhost' (using password: YES))
- ### The error may exist in sqlmap/User.xml
- ### The error may involve test.findUserById
- ### The error occurred while executing a query
- ### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Access denied for user 'Administrator'@'localhost' (using password: YES))
- at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:75)
- at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:371)
- at $Proxy5.selectOne(Unknown Source)
- at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:163)
- at test.lindl.sm.dao.impl.UserDaoImpl.findUserById(UserDaoImpl.java:18)
- at test.lindl.sm.dao.impl.UserDaoImplTest.testFindUserById(UserDaoImplTest.java:24)
- at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
- at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
- at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
- at java.lang.reflect.Method.invoke(Method.java:597)
- at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
- at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
- at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
- at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
- at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
- at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
- at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:69)
- at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)
- at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
- at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
- at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
- at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
- at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
- at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
- at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
- at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
- at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
- at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
- at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
- at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
- Caused by: org.apache.ibatis.exceptions.PersistenceException:
- ### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Access denied for user 'Administrator'@'localhost' (using password: YES))
- ### The error may exist in sqlmap/User.xml
- ### The error may involve test.findUserById
- ### The error occurred while executing a query
- ### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Access denied for user 'Administrator'@'localhost' (using password: YES))
- at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:26)
- at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:111)
- at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:102)
- at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:66)
- at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
- at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
- at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
- at java.lang.reflect.Method.invoke(Method.java:597)
- at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:358)
- ... 28 more
- Caused by: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Access denied for user 'Administrator'@'localhost' (using password: YES))
- at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80)
- at org.mybatis.spring.transaction.SpringManagedTransaction.openConnection(SpringManagedTransaction.java:81)
- at org.mybatis.spring.transaction.SpringManagedTransaction.getConnection(SpringManagedTransaction.java:67)
- at org.apache.ibatis.executor.BaseExecutor.getConnection(BaseExecutor.java:279)
- at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:72)
- at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:59)
- at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:267)
- at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:137)
- at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:96)
- at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:77)
- at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:108)
- ... 35 more
- Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Access denied for user 'Administrator'@'localhost' (using password: YES))
- at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1225)
- at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)
- at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
- at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
- ... 45 more
- Caused by: java.sql.SQLException: Access denied for user 'Administrator'@'localhost' (using password: YES)
- at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1086)
- at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237)
- at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169)
- at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:928)
- at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1750)
- at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1290)
- at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2502)
- at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2535)
- at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2320)
- at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834)
- at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
- at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
- at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
- at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
- at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
- at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
- at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416)
- at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:347)
- at org.apache.commons.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)
- at org.apache.commons.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:294)
- at org.apache.commons.dbcp.BasicDataSource.validateConnectionFactory(BasicDataSource.java:1247)
- at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1221)
- ... 48 more