默认情况下,hibernate/JPA 在server.log中记录的SQL语句,参数都是用?代替的,这样不太方便. 网上留传的p6spy在最新的jboss上(EAP 6.0+版本)貌似已经不起作用了(至少我没弄成功过)
幸好,Jboss已经自带了jdbc监控功能,开启步骤如下:
1、standalone.xml中增加
1 <logger category="jboss.jdbc.spy"> 2 <level name="DEBUG"/> 3 </logger>
2、所有的DataSouce节点上,增加spy="true",参考下面这样:
1 <datasource jta="false" jndi-name="java:/XE" pool-name="XE" enabled="true" spy="true" use-ccm="false"> 2 <connection-url>jdbc:oracle:thin:@localhost:1521:XE</connection-url> 3 <driver-class>oracle.jdbc.OracleDriver</driver-class> 4 ...
好了,server.log中就会记录详细的sql信息,类似下面这样:
...
11:26:28,788 INFO [stdout] (http-localhost/127.0.0.1:8080-2)
Hibernate:
11:26:28,788 INFO [stdout]
(http-localhost/127.0.0.1:8080-2) /*
insert
org.jboss.as.quickstart.hibernate4.model.Member
11:26:28,788
INFO [stdout]
(http-localhost/127.0.0.1:8080-2) */ insert
11:26:28,788
INFO [stdout]
(http-localhost/127.0.0.1:8080-2)
into
11:26:28,788
INFO [stdout]
(http-localhost/127.0.0.1:8080-2)
MemberHibernate4Demo
11:26:28,788 INFO [stdout]
(http-localhost/127.0.0.1:8080-2) (address, email, name, phone_number, id)
11:26:28,788 INFO [stdout]
(http-localhost/127.0.0.1:8080-2) values
11:26:28,788
INFO [stdout]
(http-localhost/127.0.0.1:8080-2) (?, ?, ?, ?,
?)
11:26:28,788 DEBUG [jboss.jdbc.spy]
(http-localhost/127.0.0.1:8080-2) java:/XE [PreparedStatement] setString(1,
shanghai,China)
11:26:28,788 DEBUG [jboss.jdbc.spy]
(http-localhost/127.0.0.1:8080-2) java:/XE [PreparedStatement] setString(2,
jimmy.yang@jboss.org)
11:26:28,789
DEBUG [jboss.jdbc.spy] (http-localhost/127.0.0.1:8080-2) java:/XE
[PreparedStatement] setString(3,
jimmy)
11:26:28,789 DEBUG [jboss.jdbc.spy]
(http-localhost/127.0.0.1:8080-2) java:/XE [PreparedStatement] setString(4,
13916752129)
11:26:28,789 DEBUG [jboss.jdbc.spy]
(http-localhost/127.0.0.1:8080-2) java:/XE [PreparedStatement] setLong(5,
4)
...