Monitor SQL and Connection running in MyBatis using Log4jdbc-log4j2

Source: Internet
Author: User
MyBatis is used in the project, a more annoying problem is unable to see the specific circumstances of the executed SQL, So, find the Log4jdbc-log4j2. This is a jdb-based

MyBatis is used in the project, a more annoying problem is unable to see the specific circumstances of the executed SQL, So, find the Log4jdbc-log4j2. This is a jdb-based

Introduction: The use of MyBatis in the project, a more annoying problem is unable to see the specific execution of the SQL, So, find the Log4jdbc-log4j2. This is a jdbc-level listening tool that can listen for the main operations on the database, so as to perfectly view the operations executed in it.

How to obtain the auto-incrementing primary key during MyBatis insertion

MyBatis quick start tutorial

1. Log4jdbc-log4j2 version Selection

2. How to introduce it to a project?

If the project is based on the Maven tool, you can directly use the following dependencies to perfectly solve the problem:


Org. bgee. log4jdbc-log4j2
Log4jdbc-log4j2-jdbcXX
1.16

3. Install dependency packages

A. If log4j is used, log4j.-core. jar and log4j-api.jar are required.

B. slf4j-api.jar is required if slf4j is used

4. Modify jdbc. url and driver. Class in the database connection.

Xxx. driverClass =Net. sf. log4jdbc. SQL. jdbcapi. DriverSpy------ Database driver
............................
Xxxx. jdbcUrl =Jdbc: log4\: Oracle \: thin \: @ 192.168.1.20 \: 1521 \: orcl -------------- database connection URL

Note: The bold content indicates the modified content.

Original URL:
Jdbc: derby: // localhost: 1527 /// db-derby-10.2.2.0-bin/databases/MyDatabase

Replaced URL:

Jdbc: log4jdbc: derby: // localhost: 1527 // db-derby-10.2.2.0-bin/databases/MyDatabase

5. Configure log4j and log4j. properties

6. Added log4jdbc. log4j2. properties.

Here we use the Slf4j API and declare it as follows:

Log4jdbc. spylogdelegator. name = net. sf. log4jdbc. log. slf4j. Slf4jSpyLogDelegator
7. Available Logger

Loggerdescription

Jdbc. sqlonlyLogs only SQL. SQL executed within a prepared statement is automatically shown with it's bind arguments replaced with the data bound at that position, for greatly increased readability.

Jdbc. sqltimingLogs the SQL, post-execution, including timing statistics on how long the SQL took to execute.

Jdbc. auditLogs all jdbc callsets t for ResultSets. This is a very voluminous output, and is not normally needed unless tracking down a specific JDBC problem.

Jdbc. resultsetEven more voluminous, because all callto ResultSet objects are logged.

Jdbc. resultsettableLog the jdbc results as a table. Level debug will fill in unread values in the result set.

Jdbc. connectionLogs connection open and close events as well as dumping all open connection numbers. This is very useful for hunting down connection leak problems.

These parameters must be configured in log4j. properties or log4j. xml.

Example:

! Log all JDBC callexcept for ResultSet CILS
Log4j. logger. jdbc. audit = DEBUG, jdbc
Log4j. additi.pdf. jdbc. audit = false

! Log only JDBC callto ResultSet objects
Log4j. logger. jdbc. resultset = DEBUG, jdbc
Log4j. additi.pdf. jdbc. resultset = false

! Log only the SQL that is executed.
Log4j. logger. jdbc. sqlonly = DEBUG, SQL
Log4j. additiloud. jdbc. sqlonly = false

! Log timing information about the SQL that is executed.
Log4j. logger. jdbc. sqltiming = DEBUG, sqltiming
Log4j. additiaming. jdbc. sqltiming = false

! Log connection open/close events and connection number dump
Log4j. logger. jdbc. connection = FATAL, connection
Log4j. additi.pdf. jdbc. connection = false


! The appender used for the jdbc api layer call logging above, SQL only
Log4j. appender. SQL = org. apache. log4j. FileAppender
Log4j. appender. SQL. File =./logs/SQL. log
Log4j. appender. SQL. Append = false
Log4j. appender. SQL. layout = org. apache. log4j. PatternLayout
Log4j. appender. SQL. layout. ConversionPattern = -----> % d {yyyy-MM-dd HH: mm: ss. SSS} % m % n

! The appender used for the jdbc api layer call logging above, SQL timing
Log4j. appender. sqltiming = org. apache. log4j. FileAppender
Log4j. appender. sqltiming. File =./logs/sqltiming. log
Log4j. appender. sqltiming. Append = false
Log4j. appender. sqltiming. layout = org. apache. log4j. PatternLayout
Log4j. appender. sqltiming. layout. ConversionPattern = -----> % d {yyyy-MM-dd HH: mm: ss. SSS} % m % n

! The appender used for the jdbc api layer call logging abve
Log4j. appender. jdbc = org. apache. log4j. FileAppender
Log4j. appender. jdbc. File =./logs/jdbc. log
Log4j. appender. jdbc. Append = false
Log4j. appender. jdbc. layout = org. apache. log4j. PatternLayout
Log4j. appender. jdbc. layout. ConversionPattern = % d {yyyy-MM-dd HH: mm: ss. SSS} % m % n

! The appender used for the JDBC Connection open and close events
Log4j. appender. connection = org. apache. log4j. FileAppender
Log4j. appender. connection. File =./logs/connection. log
Log4j. appender. connection. Append = false
Log4j. appender. connection. layout = org. apache. log4j. PatternLayout
Log4j. appender. connection. layout. ConversionPattern = % d {yyyy-MM-dd HH: mm: ss. SSS} % m % n

Summary:

After some hard work, we can finally see all kinds of operations in the database in the system, and all kinds of operations in the database are under the eyes ........

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.