【问题标题】:spring datasource connection errorsspring数据源连接错误
【发布时间】:2013-10-08 16:09:28
【问题描述】:

如果配置的数据库不存在,我在哪里可以查看数据源连接错误。我正在设置 spring security 3.1 以验证使用具有自定义模式的数据库。我花费的大部分时间是由于我的自定义 jdbc.properties 文件中数据库名称的拼写错误。

如果我注意到错误是由于这个错字造成的,那会容易得多。有没有办法配置 spring 数据源以在无法获得有效数据源连接的情况下显示错误。一种方法是检查返回的数据源引用对象并确定它是否返回 null。但基本上,我正在调查 Spring 报告的错误。

  //Spring datasource

     <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 

        <property name="driverClassName" value="${jdbc.driverClassName}" />  
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="maxActive" value="100" />
        <property name="maxIdle" value="30" /> 

 </bean>


    <sec:authentication-manager alias="authenticationManager">
   <sec:authentication-provider user-service-ref="jdbcUserService"/>
</sec:authentication-manager>


    <beans:bean id = "jdbcUserService" 
    class="com.company.portal.helper.CustomJDBCDaoImpl">
    <beans:property name="dataSource" ref="dataSource" />
     <beans:property name="enableAuthorities" value="true"/>
    <beans:property name="usersByUsernameQuery">
        <beans:value>
            select username,password,enabled from users where username = ?
        </beans:value>
    </beans:property>
    <beans:property name="authoritiesByUsernameQuery">
        <beans:value>
            select username,authority from user_roles where username = ?
        </beans:value>
    </beans:property>   
</beans:bean>

如果这可以使用 log4j 实现,请解释一下。我能够为我的项目模块设置 log4j,但不能为 spring 模块设置。如果使用新的 spring 模块(例如:spring social),是否需要更新日志记录?

感谢您的宝贵时间。

【问题讨论】:

    标签: spring datasource


    【解决方案1】:

    我不使用t know any build in way, and it is but it should be easy to implement your own. But instead of invoking the statements, it is much more easieer to invoke the completeloadUserByUsername` 方法。

    @Service
    public class StatementChecker implements ApplicationListener<ContextStartedEvent> {
    
        @Autowired
        private CustomJDBCDaoImpl jdbcDaoImpl;
    
        public void onApplicationEvent(E event) {
          String username = "iKnowYouExist";
          try {
             UserDetails userDetails = jdbcDaoImpl.loadUserByUsername(username);
          } catch (UsernameNotFoundException e) {
             LOGGER.ERROR ("the user does not exist", e);
          } catch (DataAccessException) {
             LOGGER.ERROR ("some thing is wrong with the query, database or database connetion");
          }
        }
    }
    

    但老实说,我坚信最好写一个测试用例,也许你可以重用这段代码进行测试。

    【讨论】:

    • 感谢您的意见。我将更改为一种方法,而不是使用 xml 文件中的语句来查找用户名。关于日志记录错误,我主要寻找内置插件类型或易于配置的显示不同类型的错误,因为如果手动处理,我可能无法预见/捕获所有类型的错误。
    • Spring security 比较特殊,不会因为不泄露安全相关信息而向前端泄露异常。
    猜你喜欢
    • 2019-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-11
    • 2012-04-16
    • 1970-01-01
    • 2018-06-29
    相关资源
    最近更新 更多