gpt4 book ai didi

java - 无法在 Log4j Spring 桌面应用程序中记录 java.sql.SQLException

转载 作者:行者123 更新时间:2023-12-02 06:34:40 25 4
gpt4 key购买 nike

大家好,我在桌面 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

堆栈跟踪

Exception in thread "main" org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Connection for transaction; nested exception is java.sql.SQLException: ORA-01017: invalid username/password; logon denied

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)

最佳答案

问题似乎出在 AOP 事务对象中发生异常。由于 AOP 通过创建代理对象来工作,因此异常将在那里抛出,而不是您 try catch 异常的地方。我相信虽然有一个 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

关于java - 无法在 Log4j Spring 桌面应用程序中记录 java.sql.SQLException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19782840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com