1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.xchain.namespaces.hibernate;
17
18 import javax.xml.namespace.QName;
19
20 import org.apache.commons.jxpath.JXPathContext;
21 import org.hibernate.Session;
22 import org.hibernate.Transaction;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.xchain.annotations.Attribute;
26 import org.xchain.annotations.AttributeType;
27 import org.xchain.annotations.Element;
28 import org.xchain.framework.hibernate.HibernateLifecycle;
29 import org.xchain.impl.ChainImpl;
30
31
32
33
34 @Element(localName="debug-session")
35 public abstract class DebugSessionCommand extends ChainImpl {
36 public static Logger log = LoggerFactory.getLogger(DebugSessionCommand.class);
37
38 public boolean execute(JXPathContext context) throws Exception {
39 try {
40 Session session = null;
41 Transaction transaction = null;
42
43 String message = getMessage(context);
44 log.debug(message);
45
46 QName name = getName(context);
47 session = HibernateLifecycle.getCurrentSession(name);
48
49 log.debug("Getting session for {}", name.toString());
50
51
52 if( session == null ) {
53 throw new IllegalStateException("Session not found.");
54 }
55
56
57 logSession(session);
58
59 transaction = session.getTransaction();
60
61 if (transaction != null) {
62 log.debug("Transaction Active: {}", transaction.isActive());
63 }
64 }
65 catch (Exception e) {
66 log.error("Error inspecting Session: {}", e.getMessage(), e);
67 }
68
69 return false;
70 }
71
72 private void logSession(Session session) {
73 log.debug("Connected, Dirty, Open: {}", new boolean[]{session.isConnected(), session.isDirty(), session.isOpen()});
74 log.debug("CacheMode: {}", session.getCacheMode().toString());
75 log.debug("EntityMode: {}", session.getEntityMode().toString());
76 log.debug("FlushMode: {}", session.getFlushMode().toString());
77 }
78
79 @Attribute(localName="name",
80 type=AttributeType.QNAME,
81 defaultValue= "{http://www.xchain.org/hibernate}session-factory")
82 public abstract QName getName(JXPathContext context);
83
84 @Attribute(localName="message", type=AttributeType.ATTRIBUTE_VALUE_TEMPLATE, defaultValue="")
85 public abstract String getMessage(JXPathContext context);
86 }