spring数据源连接错误

时间:2013-10-08 16:09:28

标签: spring datasource

如果配置的数据库不存在,我在哪里可以查看数据源连接错误。我正在设置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 social),是否需要更新日志记录?

感谢您的时间。

1 个答案:

答案 0 :(得分:0)

我没有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 complete loadUserByUsername`方法。

@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");
      }
    }
}

但说实话,我坚信最好编写测试用例,也许你可以重复使用这段代码进行测试。

相关问题