Skip to content

Instantly share code, notes, and snippets.

@SerCeMan
Created February 26, 2014 12:30
Show Gist options
  • Save SerCeMan/9228690 to your computer and use it in GitHub Desktop.
Save SerCeMan/9228690 to your computer and use it in GitHub Desktop.
Find acquire connections
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* @author stselovalnikov
* @since Feb 26, 2014
*/
public class StackTraceScanner
{
public static class StackTrace
{
private final String firstLine;
private final List<String> lines = new ArrayList<>();
public StackTrace(String line)
{
this.firstLine = line;
}
public void addLine(String line)
{
lines.add(line);
}
public String getFirstLine()
{
return firstLine;
}
public List<String> getLines()
{
return Collections.unmodifiableList(lines);
}
@Override
public String toString()
{
StringBuilder result = new StringBuilder(firstLine + "\n");
for (String line : lines)
{
result.append(line).append("\n");
}
return result.toString();
}
}
public static void main(String[] args) throws FileNotFoundException, IOException
{
// String path = args[0];
String path = "/home/stselovalnikov/Downloads/jstack.2";
List<StackTrace> traces = new ArrayList<>();
StackTrace current = null;
try (BufferedReader br = new BufferedReader(new FileReader(path)))
{
String line;
while ((line = br.readLine()) != null)
{
if (line.isEmpty() && current == null)
continue;
else if (line.isEmpty())
{
traces.add(current);
current = null;
}
else if (current == null)
current = new StackTrace(line);
else
current.addLine(line);
}
}
if (current != null)
{
traces.add(current);
}
System.out.println("Found " + traces.size() + " traces");
for (StackTrace stack : traces)
{
int count = 0;
for (String line : stack.getLines())
{
if (line.contains("TransactionTemplate.execute"))
{
count++;
}
}
if (count >= 2)
{
System.out.println(stack.getFirstLine());
System.out.println(count);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment