提交 62c4775d authored 作者: noelgrandin's avatar noelgrandin

display transactionStart in JMX listSessions operation, useful for debugging…

display transactionStart in JMX listSessions operation, useful for debugging long-running transactions
上级 03ef250f
...@@ -488,6 +488,7 @@ public class Session extends SessionWithState { ...@@ -488,6 +488,7 @@ public class Session extends SessionWithState {
unlinkLobMap = null; unlinkLobMap = null;
} }
unlockAll(); unlockAll();
transactionStart = 0;
} }
private void checkCommitRollback() { private void checkCommitRollback() {
...@@ -516,6 +517,7 @@ public class Session extends SessionWithState { ...@@ -516,6 +517,7 @@ public class Session extends SessionWithState {
autoCommit = true; autoCommit = true;
autoCommitAtTransactionEnd = false; autoCommitAtTransactionEnd = false;
} }
transactionStart = 0;
} }
/** /**
...@@ -834,9 +836,6 @@ public class Session extends SessionWithState { ...@@ -834,9 +836,6 @@ public class Session extends SessionWithState {
* Wait for some time if this session is throttled (slowed down). * Wait for some time if this session is throttled (slowed down).
*/ */
public void throttle() { public void throttle() {
if (currentCommandStart == 0) {
currentCommandStart = System.currentTimeMillis();
}
if (throttle == 0) { if (throttle == 0) {
return; return;
} }
...@@ -860,10 +859,14 @@ public class Session extends SessionWithState { ...@@ -860,10 +859,14 @@ public class Session extends SessionWithState {
*/ */
public void setCurrentCommand(Command command) { public void setCurrentCommand(Command command) {
this.currentCommand = command; this.currentCommand = command;
if (queryTimeout > 0 && command != null) { if (command != null) {
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
currentCommandStart = now; currentCommandStart = now;
cancelAt = now + queryTimeout; if (queryTimeout > 0) {
cancelAt = now + queryTimeout;
}
} else {
currentCommandStart = 0;
} }
} }
...@@ -1054,6 +1057,7 @@ public class Session extends SessionWithState { ...@@ -1054,6 +1057,7 @@ public class Session extends SessionWithState {
public void begin() { public void begin() {
autoCommitAtTransactionEnd = true; autoCommitAtTransactionEnd = true;
autoCommit = false; autoCommit = false;
transactionStart = System.currentTimeMillis();
} }
public long getSessionStart() { public long getSessionStart() {
...@@ -1061,9 +1065,6 @@ public class Session extends SessionWithState { ...@@ -1061,9 +1065,6 @@ public class Session extends SessionWithState {
} }
public long getTransactionStart() { public long getTransactionStart() {
if (transactionStart == 0) {
transactionStart = System.currentTimeMillis();
}
return transactionStart; return transactionStart;
} }
......
...@@ -175,14 +175,17 @@ public class DatabaseInfo implements DatabaseInfoMBean { ...@@ -175,14 +175,17 @@ public class DatabaseInfo implements DatabaseInfoMBean {
StringBuilder buff = new StringBuilder(); StringBuilder buff = new StringBuilder();
for (Session session : database.getSessions(false)) { for (Session session : database.getSessions(false)) {
buff.append("session id: ").append(session.getId()); buff.append("session id: ").append(session.getId());
buff.append(" user: ").append(session.getUser().getName()).append('\n'); buff.append("\tuser: ").append(session.getUser().getName()).append('\n');
buff.append("connected: ").append(new Timestamp(session.getSessionStart())).append('\n'); buff.append("\tconnected: ").append(new Timestamp(session.getSessionStart())).append('\n');
if (session.getTransactionStart() != 0) {
buff.append("\ttransactionStart: ").append(new Timestamp(session.getTransactionStart())).append('\n');
}
Command command = session.getCurrentCommand(); Command command = session.getCurrentCommand();
if (command != null) { if (command != null) {
buff.append("statement: ").append(session.getCurrentCommand()).append('\n'); buff.append("\tstatement: ").append(session.getCurrentCommand()).append('\n');
long commandStart = session.getCurrentCommandStart(); long commandStart = session.getCurrentCommandStart();
if (commandStart != 0) { if (commandStart != 0) {
buff.append("started: ").append(new Timestamp(commandStart)).append('\n'); buff.append("\tstarted: ").append(new Timestamp(commandStart)).append('\n');
} }
} }
Table[] t = session.getLocks(); Table[] t = session.getLocks();
......
...@@ -8,10 +8,14 @@ package org.h2.test.unit; ...@@ -8,10 +8,14 @@ package org.h2.test.unit;
import java.lang.management.ManagementFactory; import java.lang.management.ManagementFactory;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.HashMap; import java.util.HashMap;
import java.util.Set; import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import javax.management.Attribute; import javax.management.Attribute;
import javax.management.JMException;
import javax.management.MBeanAttributeInfo; import javax.management.MBeanAttributeInfo;
import javax.management.MBeanInfo; import javax.management.MBeanInfo;
import javax.management.MBeanOperationInfo; import javax.management.MBeanOperationInfo;
...@@ -34,8 +38,12 @@ public class TestJmx extends TestBase { ...@@ -34,8 +38,12 @@ public class TestJmx extends TestBase {
TestBase.createCaller().init().test(); TestBase.createCaller().init().test();
} }
@SuppressWarnings("unchecked")
public void test() throws Exception { public void test() throws Exception {
testJMX();
testTransactionsInProgress();
}
private void testJMX() throws Exception {
HashMap<String, MBeanAttributeInfo> attrMap; HashMap<String, MBeanAttributeInfo> attrMap;
HashMap<String, MBeanOperationInfo> opMap; HashMap<String, MBeanOperationInfo> opMap;
String result; String result;
...@@ -104,8 +112,8 @@ public class TestJmx extends TestBase { ...@@ -104,8 +112,8 @@ public class TestJmx extends TestBase {
conn = getConnection("jmx;jmx=true"); conn = getConnection("jmx;jmx=true");
name = new ObjectName("org.h2:name=JMX,*"); name = new ObjectName("org.h2:name=JMX,*");
Set set = mbeanServer.queryNames(name, null); Set<ObjectName> set = mbeanServer.queryNames(name, null);
name = (ObjectName) set.iterator().next(); name = set.iterator().next();
assertEquals("16384", mbeanServer.getAttribute(name, "CacheSizeMax").toString()); assertEquals("16384", mbeanServer.getAttribute(name, "CacheSizeMax").toString());
mbeanServer.setAttribute(name, new Attribute("CacheSizeMax", 1)); mbeanServer.setAttribute(name, new Attribute("CacheSizeMax", 1));
...@@ -122,4 +130,34 @@ public class TestJmx extends TestBase { ...@@ -122,4 +130,34 @@ public class TestJmx extends TestBase {
} }
private void testTransactionsInProgress() throws SQLException, InterruptedException, JMException, NullPointerException {
MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName name = new ObjectName("org.h2:name=JMX,path=mem_jmx");
deleteDb("jmx");
final Connection conn = getConnection("mem:jmx;jmx=true");
final CountDownLatch latch = new CountDownLatch(1);
new Thread("TestJmx,sleep") {
public void run() {
try {
conn.setAutoCommit(false);
Statement stat = conn.createStatement();
stat.execute("CREATE ALIAS sleep FOR \"java.lang.Thread.sleep\"");
stat.execute("begin");
latch.countDown();
stat.execute("call sleep(2000)");
} catch (SQLException ex) {}
}
}.start();
latch.await(2000, TimeUnit.MILLISECONDS);
Thread.sleep(200);
String result = mbeanServer.invoke(name, "listSessions", null, null).toString();
assertTrue(result.indexOf("transactionStart:") >= 0);
assertTrue(result.indexOf("call sleep") >= 0);
conn.close();
}
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论