-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: Showing SQL Queries On Web Page...
PostPosted: Wed May 18, 2005 2:23 am 
Newbie

Joined: Tue May 17, 2005 2:24 am
Posts: 5
Hi,

Can I display the generated Queries on the web page?

Regards,
Hibernaut


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 18, 2005 3:30 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
You can write your own Logger appender (see log4j) in such a way that you 'remember' all sql queries and than expose them to web context.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 18, 2005 3:38 am 
Newbie

Joined: Tue May 17, 2005 2:24 am
Posts: 5
Thanks alsej,

I'm trying for the same. I've already configured log4j to write the SQL queries in a separate log file, say hibernate.log.

I also have a taglib for log4j (http://jakarta.apache.org/taglibs/doc/log-doc/log-1.0/), which can dump any attribut available in request object on the page.

I'm using this taglib from JSP.

I'm expecting that I shall write a log4j appender, which can write these SQL queries onto the request object, then the available taglib will dump the values from request object to the web page.

Any further ideas are most welcome!!!

Thanks & Regards,
Hibernaut


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 18, 2005 3:50 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
Map all sql queries to current thread using ThreadLocal. Then simply 'ask' for these queries in rendering code.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 18, 2005 6:23 am 
Newbie

Joined: Tue May 17, 2005 2:24 am
Posts: 5
Thanks alesj,

Can you please elaborate, how do I use ThreadLocal to gather all the queries in a session, and then how and from where shall that be invoked to show the queries on the web page....

I'll be thankful to you, if I get some hint on this...

Regards,
Hibernaut.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 18, 2005 6:37 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
Since you probably have session per transaction/request.
Configure your logger adapter in such way that you know when sql queries are logged in Hibernate.

In your apender class:

private static ThreadLocal resources = new ThreadLocal();

public static List getQueries() {
return (List)resources.get();
}

public static void clearResources() {
resources.set(null);
}

private void addQuery(String query) {
List queries = (List) resources.get();
if (queris == null) {
queries = new ArrayList();
resources.set(queries);
}
queries.add(query);
}

Be sure to clear resources (LogAdapter.claerResources()) when finishing rendering your query list - maybe in a servlet filter, ...


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 19, 2005 3:27 am 
Newbie

Joined: Tue May 17, 2005 2:24 am
Posts: 5
I am having a HibernateUtil class, where I'm having a ThreadLocal object as:

public static final ThreadLocal session = new ThreadLocal();

I'm confused, if I need to include the piece of code you suggested in HibernateUtil class. And from the EventManager class, I'll be having the method as below:

public void store(String title, Date theDate) {
Session session = null;
Transaction tx = null;
try {
session = HibernateUtil.currentSession();
tx = session.beginTransaction();

Event theEvent = new Event();
theEvent.setTitle(title);
theEvent.setDate(theDate);

session.save(theEvent);
System.out.println("#########::: " + HibernateUtil.getQueries());
tx.commit();
//cleanup(session);
HibernateUtil.closeSession();
} catch (HibernateException e) {
e.printStackTrace();
} finally {
if(session.isOpen()){
session.close();
}
}
}

I'll instantiate the EventManager class from JSP, and invoke the method "store", then I'll change the return type from void to a list, and then retrieve the queries, and display it...


Please advice...
Hibernaut.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.