0

大家好,我在桌面 Spring 应用程序上使用 log4j。每当我故意为我的数据库连接使用无效的用户名/密码时,我都很难在我的日志文件中记录异常。我无法记录异常:java.sql.SQLException: ORA-01017: invalid username/password; 登录被拒绝

下面是 Stacktrace 和我的 log4j.properties

LOG4J 属性

# Root logger option
log4j.rootLogger=INFO, file, stdout, ERROR

# Application logging options
log4j.logger.org.apache=ERROR
log4j.logger.org.springframework.jdbc.core.JdbcTemplate=DEBUG    
log4j.logger.jdbc.resultset=ERROR
log4j.logger.jdbc.connection=ALL
log4j.logger.jdbc.resultsettable=OFF

log4j.logger.org.springframework.jdbc.datasource.DataSourceTransactionManager=ERROR
log4j.logger.org.springframework.transaction=ERROR
log4j.logger.java.sql.SQLException=ALL

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\myapp.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n


# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

堆栈跟踪

线程“主”org.springframework.transaction.CannotCreateTransactionException 中的异常:无法为事务打开 JDBC 连接;嵌套异常是 java.sql.SQLException: ORA-01017: invalid username/password; 登录被拒绝

at org.springframework.jdbc.datasource.DataSourceTransactionManager.doBegin(DataSourceTransactionManager.java:240)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:371)
at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:335)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:105)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:621)
at com.pldt.financials.service.AccountReceivableService$$EnhancerByCGLIB$$3a1d2243.generateUploadFile(<generated>)
at com.pldt.core.App.start(App.java:33)
at com.pldt.core.App.main(App.java:19)

引起:java.sql.SQLException:ORA-01017:用户名/密码无效;登录被拒绝

at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:113)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:283)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:278)
at oracle.jdbc.driver.T4CTTIoauthenticate.receiveOauth(T4CTTIoauthenticate.java:792)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:364)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:454)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:802)
at java.sql.DriverManager.getConnection(DriverManager.java:525)
at java.sql.DriverManager.getConnection(DriverManager.java:140)
at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:173)
at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriver(DriverManagerDataSource.java:164)
at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnectionFromDriver(AbstractDriverBasedDataSource.java:149)
at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection(AbstractDriverBasedDataSource.java:119)
at org.springframework.jdbc.datasource.DataSourceTransactionManager.doBegin(DataSourceTransactionManager.java:202)
4

2 回答 2

2

问题似乎在于异常发生在 AOP 事务对象中。由于 AOP 通过创建代理对象来工作,因此异常被抛出那里而不是您试图捕获异常的地方。我相信虽然有一个用于捕获异常的 AOP 注释,但我现在只能想到 @Around 注释。

我自己刚刚学习了基本的 AOP,但这里是可用注释的一个很好的总结,包括用于捕获抛出异常的注释:

@Aspect
public class AfterThrowingExample {

  @AfterThrowing("com.xyz.myapp.SystemArchitecture.dataAccessOperation()")
  public void doRecoveryActions() {
    // ...
  }

}

来自: http ://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/aop.html

于 2013-11-05T19:35:56.610 回答
0

Spring 不记录 SQL 异常。相反,它只是将它们包装在相应的 DataAccessException 中并进一步抛出。捕获和处理(例如记录)运行时异常是您的应用程序的责任。为了避免在您的代码中出现捕获,您应该在调用堆栈中尽可能高地执行此操作。

于 2013-11-05T19:34:27.983 回答