Thursday, October 29, 2009

Toggle Hibernate "Show SQL" at Runtime

Okay, the title is a bit deceiving. As far as I know, you cannot modify the value of the configuration property "hibernate.show_sql." However, that does not prohibit you from toggling at runtime whether SQL statement are output to your log files.

Why would you want to do this?
Because turning on "show_sql" will flood your logs with all the SQL generated by hibernate, making it difficult to find a particular section you might be debugging or optimizing. Being able to toggle the output of SQL allows you to enable it just for the sections of code you are investigating.

First, the solution, then I'll get to why:


import org.apache.log4j.*;

...

Logger sqlLogger = Logger.getLogger("org.hibernate.SQL");
sqlLogger.setLevel(Level.DEBUG);

... do your stuff ...

sqlLogger.setLevel(Level.OFF);

So what's going on here?
The key is knowing what "show_sql" does and that it is not the only way to log the SQL statements generated by hibernate. Namely, "show_sql" sends all SQL to the standard output, but hibernate also uses Apache Commons Logging. So, as long as you configure commons logging to use an underlying logging framework that supports changing log levels at runtime, you can toggle the output of SQL by getting a hold of the "org.hibernate.SQL" logger and bending it to your will.

47 comments:

Anonymous said...

This i great, used it in my own development work at our Swedish yellow pages project

Anonymous said...

This is a smart blog. I mean it. You have so much knowledge about this issue, and so much passion http://www.tirtawisesa.com/

mas jangkrik said...

Cool post very informative I just found your site and read through a few posts although this is my first comment, i'll be including it in my favorites and visit again for sure. jual glutera

crocs surabaya said...


This site is awesome. My spouse and i constantly come across a new challenge & diverse in this article.

AdiB said...

This is great but it will not give you the bind variables value.
Seems like this is impossible in runtime due to NullableType.IS_TRACE_ENABLED

distributor baju anak said...

I would like to apprentice at the same time as you amend your site, how can i subscribe for a weblog site?

mas jangkrik said...

Thank you for the great article I did enjoyed reading it, I want to encourage that you continue your great job, have a good day. paket umroh murah

acaiberry asli said...

Wow this post give truly quality information . Really very useful tips are provided here .

mas jangkrik said...

Thanks for sharing this great article! http://www.selempang.com/

mas jangkrik said...

Wow...there are more valuable information in this site. we can learn more from this site. Thanks for share jasa pembuatan website

mas jangkrik said...

Absolutely fantastic posting! Lots of useful information and inspiration, both of which we all need! Keep Post ^_^ jasa pembuatan toko online

Anonymous said...

Took me time to read all the comments, but I really enjoyed the article. Grow Up

Anonymous said...

Great info. I love all the posts, I really enjoyed, I would like more information about this, pulau tidung

mas jangkrik said...

Oh! So nice. You did really so wonderful job. You have posted very informative thing. I am excitedly waiting for you next post. jual glutera

mas jangkrik said...

Hello, i am not accustomed to passing comments on discussions, but i am quite astounded by this site, so i will definitely visit again produk pemutih gigi

mas jangkrik said...

Iam very happy to reading, Hope you continue to share your information more produk penghilang jerawat

mas jangkrik said...

Great article, Very good and useful information. Iam very happy visit your site. Thanks for sharing. acai berry murah

mas jangkrik said...

Great!!! I have gain a lot from your article. thanks so much you provide really well information. visit here

Aney Collmeyer said...

We are having high interest to supply the reader points that cover the re enter values to control it.
buy facebook likes
buy youtube views

Aney Collmeyer said...

This content were widely shared since it has the desire quality on the social platform.
buy instagram followers
buy instagram followers

mas jangkrik said...

I really fun with your Blog, I would like more information about this, because it is very nice., Thank You www.sewamobiljogja.biz

mas jangkrik said...

Awesome information, Any way I'll be subscribing to your feed and I hope you post again soon. Keep Blogging http://www.momocrocs.com/

mas jangkrik said...

Amazing, it's contain great and unique information. I like to share it with all my friends. Thank jual emilay

Lauren anderson said...

Thanks for the good article I did enjoyed reading it, I need to encourage that you just continue your nice job, have a nice day. Exclusive Company Escorts

mas jangkrik said...

You've written nice post, I am gonna bookmark this page, thanks for info. Thumbs Up jasa sewa mobil jogja

kakang kokong said...

thanks for inspiring me :) .... mobil sedan corolla

Herbalmedika Online said...

The material articles is very foreign to me, but tasty as well read, greetings successful

Obat Herbal

Slamet Obama said...

I would like to apprentice at the same time as you amend your site, how can i subscribe for a weblog site?
pusat baju anak

James Smith said...

This works perfectly for the right time after the last great chance. Well optimized content too.

pengolahan air bersih said...

This is a smart blog. I mean it. You have so much knowledge about this issue, and so much passion

druze catering said...

Amazing.. this is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Thanks for sharing

Baju Anak Murah said...

Thank you untuh this article. really helped me to increase knowledge about this issue.

Otomotif Bandung said...

Such a nice information you shared in this article, I really like it.

Honda Mobilio Bandung
Suzuki Ertiga Bandung
Chevrolet Spin Bandung
Daihatsu Xenia Bandung
Pajero Sport Bandung
Honda Mobilio Bandung

Bridal saree said...

I very much to like and agree with your point of view. Thank you for sharing. At the same time, I love best pram very much .Welcome to look at my website and blog articles.

quya said...

Its such an amazing post, I really like it.

http://hargamobilbarubandung.wordpress.com/
http://hargamobilbarubandung.wordpress.com/category/chevrolet/
http://hargamobilbarubandung.wordpress.com/category/daihatsu/
http://hargamobilbarubandung.wordpress.com/category/honda/
http://hargamobilbarubandung.wordpress.com/category/suzuki/
http://hargamobilbarubandung.wordpress.com/category/mitsubishi/
http://hargamobilbarubandung.wordpress.com/category/mazda/
http://hargamobilbarubandung.wordpress.com/category/ford/
http://hargamobilbarubandung.wordpress.com/category/kia/
http://hargamobilbarubandung.wordpress.com/category/toyota/
http://hargamobilbarubandung.wordpress.com/category/nissan/

Dian Rahadian said...

Its such an amazing post, I really like it.

http://hargamobilbarubandung.wordpress.com/
http://hargamobilbarubandung.wordpress.com/category/chevrolet/
http://hargamobilbarubandung.wordpress.com/category/daihatsu/
http://hargamobilbarubandung.wordpress.com/category/honda/
http://hargamobilbarubandung.wordpress.com/category/suzuki/
http://hargamobilbarubandung.wordpress.com/category/mitsubishi/
http://hargamobilbarubandung.wordpress.com/category/mazda/
http://hargamobilbarubandung.wordpress.com/category/ford/
http://hargamobilbarubandung.wordpress.com/category/kia/
http://hargamobilbarubandung.wordpress.com/category/toyota/
http://hargamobilbarubandung.wordpress.com/category/nissan/

Dian Rahadian said...

Its such an amazing post, I really like it.

Harga Mobil Murah Bandung
Harga Mobil Chevrolet Bandung
Harga Mobil Daihatsu
Harga Mobil baru Honda
Harga Mobil Murah Suzuki
Harga Mobil baru Mitsubishi
Harga Mobil Murah Mazda
Harga Mobil Ford
Harga Mobil Kia Bandung
Harga Mobil baru Toyota
Harga Mobil Murah Nissan

Imron Rosadi said...

Looks greats, thanks for shared aneka resep kue basah praktis

aneka resep kue basah said...

Always looks amazing aneka resep kue basah praktis

karya ismail said...

This is a great article, Thanks for giving me this information. Keep posting
Furniture Jepara :
Mebel Jepara :
Furniture Jepara Murah :
Furniture Minimalis :
Furniture Jati Jepara :
Furniture Online :
Furniture Jati Jepara :
Furniture Murah :
Furniture Kursi :
Furniture Jati Jepara :
Karya Priboemi Jepara :
Furniture Kursi Jati :
Furniture jepara Online :
Furniture Online jepara :
Furniture Meja :
Furniture Online :
Kamar Set Minimalis
Kursi Tamu Mewah
Meja Makan Jati Jepara
Tempat Tidur Jati Jepara
Mebel Jati Jepara
Mebel Jepara
Kamar Set Jati

sewa mobil surabaya said...

sewa mobil surabaya, kami berkomitmen untuk memberikan harga ekonomis untuk pelanggan dengan harga sewa paling murah di surabaya, tentunya dengan tidak mengurangi kualitas pelayanan untuk pelanggan. Cs: 03171500666 / 03181500666 / 085232729729

www.sriwijayarentcar.com

Malih Tongtong said...

This is my very first time i visit right here. I found so numerous interesting stuff in your weblog particularly its discussion. harga mobil honda harga honda mobilio baru

Pusat Les Privat said...

This is really wonderful website. If you need this rental mobil di Pare and also you can find this Les privat guru ke rumah siswa di Surabaya

Pusat Les Privat said...

This website is really wonderful . If you need this
guru les privat ke rumah di Surabaya
Les privat guru ke rumah siswa di Jogja
ojek dan rental motor di Pare kampung inggris
rental motor dan ojek di Pare Kediri
Kursus Bahasa Inggris di Pare Kediri
guru les privat bahasa inggris ke rumah
Les privat Surabaya
Guru les privat TK di Surabaya
Les privat untuk SD di Surabaya
Les privat SMP/MTS di Surabaya
Les privat SMA/MA/SMK di Surabaya
Les privat TK,SD,SMP,SMA,Mahasiswa,Karyawan, dan Umum di Surabaya
Penyedia Guru les privat semua mata pelajaran di surabaya

Mukhamad Lubab said...

bisnis ebook. baca disini.
Baca ebook novel cinta. baca disini.
Baca Jual mukena al gani?. baca disini saja.
Baca Penjelasan mukena al gani, baca disini
Bacatentang memulai bisnis. baca disini.
Baca tips bisnis elbuy. baca disini
ebook novel? baca disini

xjd7410@gmail.com said...

polo ralph lauren
true religion outlet
burberry outlet
nfl jerseys wholesale
kobe bryant shoes
coach factory outlet
kobe 10 shoes
vans shoes sale
true religion jeans sale
instyler max
oakley sunglasses
gucci belts
michael kors outlet clearance
louis vuitton outlet
jordan 11 concord
coach outlet online
toms outlet
toms shoes
jordan 3 white cenment
kevin durant shoes 7
jordan retro
polo outlet
gucci outlet
cheap air jordans
jordan 4 toro
coach outlet
ray ban sunglasses
jordan 3
gucci handbags
chenyingying20160806

aaa kitty20101122 said...

air jordan
michael kors handbags
kyrie irving shoes
led shoes for kids
links of london outlet store
lebron 14
light up shoes
yeezy boost
michael kors outlet
nike air huarache