您的位置:首页 > 其它

ssm整合过程中问题记录--异常:Access denied for user 'Administrator'@'localhost' (using password: YES)

2017-04-20 00:00 896 查看
1、操作过程:

spring整合mybatis+mysql,所有配置与代码都以完成,进行junit单元测试的。

2、异常现象:

启动junit测试接口方法,出现以下异常:


警告:com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@4c9258a6--AcquisitionAttemptFailed!!!

Clearingpendingacquires.Whiletryingtoacquireaneedednewresource,

wefailedtosucceedmorethanthemaximumnumberofallowedacquisitionattempts(2).Lastacquisitionattemptexception:

java.sql.SQLException:Accessdeniedforuser'Administrator'@'localhost'(usingpassword:YES)

3、分析步骤:

3.1解析错误信息:

Accessdeniedforuser'Administrator'@'localhost'

这一句是很重要的信息,很奇怪的是原本配置的root用户,结果错误日志显示的是Administrator。

3.2检查配置,并没有什么问题。

配置的jdbc如下:


jdbc.properties文件

driver=com.mysql.jdbc.Driver url=jdbc:mysql://127.0.0.1:3306/seckill?useUnicode=true&characterEncoding=UTF-8 username=root
password=123456


spring-dao.xml配置文件目录

[html]viewplaincopy

<!--2、数据库连接池-->

<beanid="dataSource"class="com.mchange.v2.c3p0.ComboPooledDataSource">

<!--配置连接池属性-->

<propertyname="driverClass"value="${driver}"/>

<propertyname="jdbcUrl"value="${url}"/>

<propertyname="user"value="${username}"/>

<propertyname="password"value="${password}"/>

<!--c3p0连接池私有属性-->

<propertyname="maxPoolSize"value="30"/>

<propertyname="minPoolSize"value="10"/>

<propertyname="autoCommitOnClose"value="false"/>

<!--获取链接超时时间-->

<propertyname="checkoutTimeout"value="1000"/>

<!--重试次数-->

<propertyname="acquireRetryAttempts"value="2"/>

</bean>

3.3怀疑spring加载属性文件的过程中,username没有加载成功;

1)尝试将username写死在xml配置中,即:

[html]viewplaincopy

<propertyname="user"value="${username}"/>

改为

[html]viewplaincopy

<propertyname="user"value="root"/>

[html]viewplaincopy

结果问题解决,没有出现异常。

[html]viewplaincopy

但是driver、url、password仍然是采用${}的引用

[html]viewplaincopy

</pre><prename="code"class="html">2)尝试将username改为jdbc,username,即:

[html]viewplaincopy

jdbc.properties中

[html]viewplaincopy

username=root

改为

[html]viewplaincopy

jdbc.username=root


相应的将xml中


[html]viewplaincopy

<propertyname="user"value="${username}"/>

改为


[html]viewplaincopy

<prename="code"class="html"><propertyname="user"value="${jdbc.username}"/>

[html]viewplaincopy

仍然OK,没有出现异常。


[html]viewplaincopy

<prename="code"class="html"><spanstyle="color:#cc0000;"><strong>所以确定是spring加载过程中出现的问题</strong></span>,导致username的值不对,又或者是关键字,百度了很多,没有找到解释,就放弃了,自己摸索下。


[html]viewplaincopy

</pre><prename="code"class="html">3.4为了弄清楚具体怎么回事,我debug找通过源码查看加载过程

[html]viewplaincopy

</pre><prename="code"class="html"><spanstyle="color:#009900;"><strong>感谢idea的IDE的强大功能,能够提示下载源码。</strong></span>

[html]viewplaincopy

<spanstyle="color:#009900;"><strong>

</strong></span>

[html]viewplaincopy

至于从哪里开始:可以再细看日志,其实是有提示:

[html]viewplaincopy

五月29,20162:16:57下午org.springframework.context.support.<strong>PropertySourcesPlaceholderConfigurer</strong>loadProperties

信息:Loadingpropertiesfilefromclasspathresource[jdbc.properties]

[html]viewplaincopy

</pre><prename="code"class="html">直接搜索这个类<prename="code"class="html"><strong>PropertySourcesPlaceholderConfigurer</strong>进去之后,发现有很多方法,我采取的比较蠢的方法,在有Property这个单词的方法中都打了个断点,

[html]viewplaincopy

再次debug启动test,

[html]viewplaincopy

<prestyle="widows:auto;"><prename="code"class="java"style="font-family:宋体;font-size:10.5pt;line-height:21px;background-color:rgb(255,255,255);">@Override

publicvoidpostProcessBeanFactory(ConfigurableListableBeanFactorybeanFactory)throwsBeansException{

if(this.propertySources==null){

this.propertySources=newMutablePropertySources();

if(this.environment!=null){

this.propertySources.addLast(

newPropertySource<Environment>(ENVIRONMENT_PROPERTIES_PROPERTY_SOURCE_NAME,this.environment){

@Override

publicStringgetProperty(Stringkey){

returnthis.source.getProperty(key);

}

}

);

}

try{

PropertySource<?>localPropertySource=

newPropertiesPropertySource(LOCAL_PROPERTIES_PROPERTY_SOURCE_NAME,mergeProperties());

if(this.localOverride){

this.propertySources.addFirst(localPropertySource);

}

else{

this.propertySources.addLast(localPropertySource);

}

}

catch(IOExceptionex){

thrownewBeanInitializationException("Couldnotloadproperties",ex);

}

}

processProperties(beanFactory,<spanstyle="color:#cc0000;">newPropertySourcesPropertyResolver(this.propertySources));</span>

this.appliedPropertySources=this.propertySources;

}

同时,自己查看变量的值,确定下来所有的配置properties都在this.propertySources

其中包含2部分:环境变量和本地变量








this.propertiesSources

--propertyList

--0[name='environmentProperties']

--source

--propertySources

--1[name='systemEnvironment']

--source

在这个属性下面发现了同为username=Administrator的环境变量;


在localProperty下的username=root

两者都是同一个key;

进一步,探究怎么会取的Administrator而不是root继续debug看之前提到的那个方法中的红色部分代码;

[java]viewplaincopy

PropertySourcesPropertyResolver这个类中这个方法:

<T>TgetProperty(Stringkey,Class<T>targetValueType,booleanresolveNestedPlaceholders)
这个方法会遍历this.propertySources这个对象,当找到了username之后就直接返回了,而按照解析的步骤,先解析的environmentProperty的属性,所以就将username赋值为了
username=Administrator而不会去解析localProperty中的username。



3.5解决方法:
分析了那么多最终是要解决的;
推荐的方法就是,避免取名与环境变量一致,如果保证不一致,那就写复杂点吧。
所以一般下面这种写法也就够了,这也是为什么网上很多都采用这种写法,今天算是明白的不这么写的隐患了。

jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://127.0.0.1:3306/seckill?useUnicode=true&characterEncoding=UTF-8jdbc.username=rootjdbc.password=123456




总结:问题来了,能够自己解决确实学到的非常多,由于使用ssm框架不熟练,很容易出现各种各样的问题,我都会尽量靠自己去解决,同时分享出来,
也希望有各位能够指出不足,提出更好的方法。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐