[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
RE: [eclipselink-users] Logging SQL results - SessionEventAdapter workaround
|
Gili,
Workaround is to use a SessionEventListener as suggested by James
http://wiki.eclipse.org/Configuring_a_Session_%28ELUG%29#How_to_Configure_Session_Event_Listeners_Using_Java
James, thank you for the pointer on using a SessionEventAdapter inside a SessionCustomizer.
I attached a patch to bug# 248188 http://bugs.eclipse.org/248188 around where we would log ResultSet Vector rows as I was only able to get the Entity using this workaround because the ResultSet was closed - you need the raw DatabaseRecord strings below these logs that show each database column [EL_DATAOBJECT.ID => 3] instead of [DataObject@27988400( id: 3].
Notice the "ResultSet Row" entry logs above (via toString() override) - multiple versions because I have fetch=FetchType.EAGER on the Entity.
[EL Finer]: 2008.09.23 10:03:26.187--ServerSession(18450577)--Thread(Thread[main,5,main])--ResultSet Row: org.eclipse.persistence.example.jpa2.embeddable.business.DataObject@9996039( id: 1 type: root date: 2008-09-23 parent: null children: null)
[EL Finer]: 2008.09.23 10:03:26.187--ServerSession(18450577)--Thread(Thread[main,5,main])--ResultSet Row: org.eclipse.persistence.example.jpa2.embeddable.business.DataObject@29945686( id: 2 type: child1 date: 2008-09-23 parent: DataObject@9996039 children: null)
[EL Finer]: 2008.09.23 10:03:26.187--ServerSession(18450577)--Thread(Thread[main,5,main])--ResultSet Row: []
[EL Finer]: 2008.09.23 10:03:26.203--ServerSession(18450577)--Thread(Thread[main,5,main])--ResultSet Row: [org.eclipse.persistence.example.jpa2.embeddable.business.DataObject@27988400( id: 3 type: gchild1 date: 2008-09-23 parent: DataObject@29945686 children: [])]
[EL Finer]: 2008.09.23 10:03:26.203--ServerSession(18450577)--Thread(Thread[main,5,main])--ResultSet Row: [org.eclipse.persistence.example.jpa2.embeddable.business.DataObject@29945686( id: 2 type: child1 date: 2008-09-23 parent: DataObject@9996039 children: [org.eclipse.persistence.example.jpa2.embeddable.business.DataObject@27988400( id: 3 type: gchild1 date: 2008-09-23 parent: DataObject@29945686 children: [])])]
However, the event.getResult() Object is already an Entity at this point and not the raw Vector of ResultSet data that you require which is closed at this point,
it would look like this in DatabaseAccessor.basicExecuteCall():632
[DatabaseRecord(
EL_DATAOBJECT.ID => 1
EL_DATAOBJECT.DATE_STAMP => 2008-09-22
EL_DATAOBJECT.NAME => root
EL_DATAOBJECT.PARENT => null),
DatabaseRecord(
EL_DATAOBJECT.ID => 2
EL_DATAOBJECT.DATE_STAMP => 2008-09-22
EL_DATAOBJECT.NAME => child1
EL_DATAOBJECT.PARENT => 1),
DatabaseRecord(
EL_DATAOBJECT.ID => 3
EL_DATAOBJECT.DATE_STAMP => 2008-09-22
EL_DATAOBJECT.NAME => gchild1
EL_DATAOBJECT.PARENT => 2)]
Here is an example of a SessionEventAdapter registered by a SessionCustomizer
Persistence.xml
<property name="eclipselink.session.customizer" value="org.eclipse.persistence.example.jpa2.embeddable.integration.JPAEclipseLinkSessionCustomizer"/>
package org.eclipse.persistence.example.jpa2.embeddable.integration;
import org.eclipse.persistence.config.SessionCustomizer;
import org.eclipse.persistence.logging.SessionLog;
import org.eclipse.persistence.sessions.Session;
import org.eclipse.persistence.sessions.SessionEvent;
import org.eclipse.persistence.sessions.SessionEventAdapter;
/**
* Add a SessionEventAdapter via SessionCustomizer to the JPA PersistenceUnit
* to enable logging of the ResultSet after ExecuteQuery
* See
* http://wiki.eclipse.org/Configuring_a_Session_%28ELUG%29#How_to_Configure_Session_Event_Listeners_Using_Java
*/
public class JPAEclipseLinkSessionCustomizer implements SessionCustomizer {
public void customize(Session session) throws Exception {
try {
SessionEventAdapter myEventListener = new SessionEventAdapter() {
// Listen for PostExecuteQuery events
public void postExecuteQuery(SessionEvent event) {
Object aResult = event.getResult();
Session aSession = event.getSession();
// Call the handler routine
// 248188: Log resultSet rows
if ((null != aResult) && aSession.shouldLog(SessionLog.ALL, SessionLog.SQL)) {// Avoid printing if no logging required.
//aSession.log(SessionLog.ALL, SessionLog.SQL, aResult.toString(), (Object[])null, this, false);
aSession.logMessage("ResultSet Row: " + aResult.toString());
}
}
};
session.getEventManager().addListener(myEventListener);
} catch (Exception e) {
e.printStackTrace();
}
}
}
-----Original Message-----
From: James Sutherland [mailto:jamesssss@xxxxxxxxx]
Sent: Monday, September 22, 2008 09:47
To: eclipselink-users@xxxxxxxxxxx
Subject: RE: [eclipselink-users] Logging SQL results
ALL is the same as FINEST, EclipseLink never logs the results, as this could
generate a lot of stuff. I agree it is sometimes useful though, so feel
free to log a bug for this. You can log the results yourself using a
SessionEventListener (postExecuteQuery()).
cowwoc wrote:
>
>
> Ah! :) Can someone please add this to the documentation?
> http://wiki.eclipse.org/EclipseLink/Examples/JPA/Logging doesn't mention
> this logging level or what extra it will log.
>
> Thanks,
> Gili
>
>
> Thomas Paradies wrote:
>>
>> Gili,
>>
>> set eclipselink.logging.level to the finest level (ALL).
>>
>> Thomas
>>
>> -----Original Message-----
>> From: eclipselink-users-bounces@xxxxxxxxxxx on behalf of cowwoc
>> Sent: Mon 9/22/2008 5:52 AM
>> To: eclipselink-users@xxxxxxxxxxx
>> Subject: Re: [eclipselink-users] Logging SQL results
>>
>>
>> I tracked this problem down to a bug in the way warp-persist is handling
>> Transaction-per-Request. The same EntityManager was being reused across
>> different HTTP requests leading to the following behavior:
>>
>> - Request goes to Thread 1. It saves version 10
>> - Request goes to Thread 2. It saves version 11.
>> - Request goes to Thread 1. The query returns the cached object (version
>> 10). When the thread attempts to commit this a OptimisticLockException is
>> thrown.
>>
>> I'd still to know how to get EclipseLink to log SQL query results
>> though...
>>
>> Gili
>> --
>>
>
>
-----
---
http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland
http://www.eclipse.org/eclipselink/
EclipseLink , http://www.oracle.com/technology/products/ias/toplink/
TopLink
Wiki: http://wiki.eclipse.org/EclipseLink EclipseLink ,
http://wiki.oracle.com/page/TopLink TopLink
Forums: http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink ,
http://www.nabble.com/EclipseLink-f26430.html EclipseLink
Book: http://en.wikibooks.org/wiki/Java_Persistence Java Persistence
--
View this message in context: http://www.nabble.com/Logging-SQL-results-tp19600126p19608296.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.
_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users