Hibernate Redirection Error from my SystemOut.log File

I have a little problem with my hibernate registration configuration. In our application, we have two threads that try to simultaneously set a lock for each row of the database table. sometimes one of these threads tries to lock a row that is already locked. This error is thrown:

[2/24/12 15:00:34:492 CET] 0000003a JDBCException W org.hibernate.util.JDBCExceptionReporter logExceptions SQL Error: 54, SQLState: 61000 [2/24/12 15:00:34:496 CET] 0000003a JDBCException E org.hibernate.util.JDBCExceptionReporter logExceptions ORA-00054: resource busy and acquire with NOWAIT specified 

these lines are placed in the SystemOut.log files. I am trying to put them in another file. So in my Log4j configuration file, I created a new application like this:

 <appender name="JDBCExceptionReporter" class="org.apache.log4j.RollingFileAppender"> <param name="Encoding" value="UTF-8"/> <param name="File" value="JDBCExceptionReporter.log"/> <param name="MaxFileSize" value="50000KB"/> <param name="MaxBackupIndex" value="5"/> <param name="BufferedIO" value="false"/> <param name="ImmediateFlush" value="true"/> <param name="Append" value="true"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d, %-5p, %X{ORG_NAME}, %X{USER_NAME}, %c - %m%n"/> <!-- Use pattern below if it is required to view the log files --> <!-- using LogFactor5--> <!--<param name="ConversionPattern" value="[slf5s.start] %d[slf5s.DATE] %-5p[slf5s.PRIORITY] %X{ORG_NAME} %X{USER_NAME} [slf5s.NDC] %c[slf5s.CATEGORY] - %m[slf5s.MESSAGE] %n"--> </layout> </appender> 

Finally, I added these loggers immediately before the root element:

 <logger name="org.hibernate" additivity="false"> <level value="ALL"/> <appender-ref ref="JDBCExceptionReporter"/> </logger> <logger name="org.hibernate.type" additivity="false"> <level value="ALL"/> <appender-ref ref="JDBCExceptionReporter"/> </logger> <logger name="org.hibernate.tool.hbm2ddl" additivity="false"> <level value="ALL"/> <appender-ref ref="JDBCExceptionReporter"/> </logger> <logger name="org.hibernate.pretty" additivity="false"> <level value="ALL"/> <appender-ref ref="JDBCExceptionReporter"/> </logger> <logger name="org.hibernate.cache" additivity="false"> <level value="ALL"/> <appender-ref ref="JDBCExceptionReporter"/> </logger> <logger name="org.hibernate.transaction" additivity="false"> <level value="ALL"/> <appender-ref ref="JDBCExceptionReporter"/> </logger> <logger name="org.hibernate.jdbc" additivity="false"> <level value="ALL"/> <appender-ref ref="JDBCExceptionReporter"/> </logger> <logger name="org.hibernate.hql.ast.AST" additivity="false"> <level value="ALL"/> <appender-ref ref="JDBCExceptionReporter"/> </logger> <logger name="org.hibernate.secure" additivity="false"> <level value="ALL"/> <appender-ref ref="JDBCExceptionReporter"/> </logger> <logger name="org.hibernate.SQL" additivity="false"> <level value="ALL"/> <appender-ref ref="JDBCExceptionReporter"/> </logger> 

This configuration does not work, and two lines are always registered in SystemOut.log, and not in my JDBCExceptionReporter.log. Does anyone have an answer to my problem? thank you for your help.

+4
source share
1 answer

Hibernate does not use Log4J directly. This is why your messages get into the WebSphere SystemOut.log file.

Prior to version 3.2, sleep mode is used to write to the Jakarta Commons Logging as a logging facade. Starting with version 3.3 and higher, he switched to SLF4J (Simple Logging Facade for Java). Since Jakarta commons logging (JCL), SLF4J is simply a facade that is often associated with other logging systems such as Java Logging or, in this case, Log4J.

I understand that your application already uses Log4J. I also assume that you are using Hibernate 3.3 or newer.

First of all, you need to add SLF4J support to your project. You do this by adding these dependencies (using Ivy, Maven, or whatever you like):

slf4j-api.jar (Core SLF4J packages)

slf4j-log4j12.jar (binds SLF4J to Log4J)

Thus, when Hibernate logs a message, SLF4J will capture it and redirect it to Log4J.

Now, when it comes to your own application code, you have two approaches.

1: Keep your classes directly interacting with Log4J classes (namely Logger and LoggerFactory)

This way you keep your classes directly by reference to Log4J Logger, for example:

 Logger logger = Logger.getLogger("com.foo"); // from package org.apache.log4j 

So, in a word, Hibernate will use SLF4J -> Log4J, and you will use Log4J directly

2: The second approach is for your code to use SLF4J. This hasnโ€™t changed so much, since you may only have to change one line of code in each of your classes that generates log messages:

 Logger logger = LoggerFactory.getLogger("com.foo"); // from package org.slf4j 

If you are using an older version of Hibernate (3.2 or older), you will have to trick it into not using Jakarta Commons Logging. First, you will have to clean up the JAR files that write the JAR files from your dependencies (manually or with an exception if you use any dependency manager). Then you will need to add the SLF4J shared port to your class path:

add jcl-over-slf4j-xyzjar (xyz is its version. For example, I use jcl-over-slf4j-1.6.1.jar ).

This jar has the exact classes and packages of the old commons-logging.jar, thereby effectively tricking all community-dependent applications for registering on SLF4J.

+1
source

Source: https://habr.com/ru/post/1398235/


All Articles