Skip to content

Commit

Permalink
Fix bug for MS SQL, MySQL
Browse files Browse the repository at this point in the history
  • Loading branch information
Gonchik Tsymzhitov authored and Gonchik Tsymzhitov committed Mar 21, 2020
1 parent 137168b commit 6bab2a5
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 32 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ To run the SQL benchmark:

or

java -cp support-tools.jar:com.atlassian.util.benchmark.JIRASQLPerformance:/path/to/your/jdbc-driver.jar \
com.atlassian.util.benchmark.JIRASQLPerformance
user passwrod driverclass jdbc-url driver-classname [numberOfRuns]
java -cp support-tools.jar:com.atlassian.util.benchmark.JIRASQLPerformance:/path/to/your/jdbc-driver.jar com.atlassian.util.benchmark.JIRASQLPerformance user passwrod driverclass jdbc-url driver-classname [numberOfRuns]

To run DB status:

Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/atlassian/util/JiraDatabaseConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public JiraDatabaseConfig(String username, String password, String url, String d
/**
* Discovers Jira DB config and loads drivers in classpath
*
* @param jiraHome home directory
* @param jiraHome home directory
* @param jiraInstallDir installation directory
* @return configuration of config
* @throws IOException
Expand Down Expand Up @@ -72,7 +72,7 @@ public static JiraDatabaseConfig parseDBConfig(String jiraHome) {

Element passwordChildren = jdbc.getFirstChildElement("password");
if (passwordChildren.getChildCount() > 0) {
password = usernameChildren.getChild(0).getValue();
password = passwordChildren.getChild(0).getValue();
}

String driverClass = jdbc.getFirstChildElement("driver-class").getChild(0).getValue();
Expand Down Expand Up @@ -113,20 +113,20 @@ private String getMinimalDBType() {

/**
* Loads database drives into the system class loader
*
* @param jiraInstallDir Jira installation directory
* @throws IOException
*/
public void loadJar(String jiraInstallDir) throws IOException {
Path libPath = Paths.get(jiraInstallDir, "lib");
Path jarPath = findJarForDB(libPath);

URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
Class sysclass = URLClassLoader.class;

System.out.println("\tDriverClass path: " + jarPath.toString());
URLClassLoader sysLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
Class<URLClassLoader> sysClass = URLClassLoader.class;
try {
Method method = sysclass.getDeclaredMethod("addURL", parameters);
Method method = sysClass.getDeclaredMethod("addURL", parameters);
method.setAccessible(true);
method.invoke(sysloader, jarPath.toUri().toURL());
method.invoke(sysLoader, jarPath.toUri().toURL());
} catch (Throwable t) {
t.printStackTrace();
throw new IOException("Error, could not add URL to system classloader");
Expand Down
23 changes: 17 additions & 6 deletions src/main/java/com/atlassian/util/benchmark/ConnectionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
import static org.apache.commons.lang3.Validate.notNull;

public class ConnectionFactory {
private final String userName;
private final String username;
private final String password;
private final String url;
private final String driverClass;
private Connection connection;

public ConnectionFactory(String username, String password, String url, String driverClass) {
notNull(username);
Expand All @@ -21,12 +23,14 @@ public ConnectionFactory(String username, String password, String url, String dr
// Check that driver class is accessible
try {
Class.forName(driverClass);
} catch (ClassNotFoundException e) {
} catch (Exception e) {
System.out.println("Where is JDBC Driver? Include in your library path, please!");
throw new IllegalArgumentException(e);
}
this.userName = username;
this.username = username;
this.password = password;
this.url = url;
this.driverClass = driverClass;
}

public ConnectionFactory(JiraDatabaseConfig config) {
Expand All @@ -37,13 +41,20 @@ public ConnectionFactory(JiraDatabaseConfig config) {
* Attempts to establish a connection to the given database URL.
* The <code>DriverManager</code> attempts to select an appropriate driver from
* the set of registered JDBC drivers.
*
* @return a connection
*/
public Connection getConnection() {
if (connection !=null){
return connection;
}
try {
return DriverManager.getConnection(url, userName, password);
} catch (SQLException e) {
throw new RuntimeException(e);
return connection = DriverManager.getConnection(url, username, password);
} catch (final SQLException e) {
System.out.println("URL: " + url);
System.out.println("Username: " + username);
System.out.println("Password: " + password);
throw new RuntimeException("Error running SQL: " + e.getMessage(), e);
}
}

Expand Down
36 changes: 21 additions & 15 deletions src/main/java/com/atlassian/util/benchmark/JIRASQLPerformance.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,16 @@ public void call() throws Exception {

private List<TimedTestRunner> getTests() throws Exception {
final List<Long> ids = new ArrayList<>(issueCount);
final Connection conn = connectionFactory.getConnection();
final Random rnd = new Random();

final Connection connection = connectionFactory.getConnection();
if (connection.isValid(10)) {
System.out.println("Connection status is valid.");
} else {
System.out.println("Connection is not valid.");
}
final Random random = new Random();
{
final PreparedStatement countIssues = conn.prepareStatement("SELECT count(*) FROM jiraissue");
final PreparedStatement countIssues = connection.prepareStatement("SELECT count(*) FROM jiraissue");
ResultSet rs = countIssues.executeQuery();
rs.next();
int noOfIssues = rs.getInt(1);
Expand All @@ -103,9 +109,9 @@ private List<TimedTestRunner> getTests() throws Exception {
// generate issueCount ints between zero and totalNoOfIssues
Set<Integer> picks = new HashSet<>(issueCount);
while (picks.size() < issueCount) {
picks.add(rnd.nextInt(noOfIssues));
picks.add(random.nextInt(noOfIssues));
}
final PreparedStatement selectIDs = conn.prepareStatement("SELECT id FROM jiraissue");
final PreparedStatement selectIDs = connection.prepareStatement("SELECT id FROM jiraissue");
rs = selectIDs.executeQuery();

for (int i = 0; i < noOfIssues; i++) {
Expand All @@ -116,34 +122,34 @@ private List<TimedTestRunner> getTests() throws Exception {
}
}
// General Issues tests
final PreparedStatement selectIssue = conn.prepareStatement("SELECT * FROM jiraissue WHERE id = ?");
final PreparedStatement selectIssue = connection.prepareStatement("SELECT * FROM jiraissue WHERE id = ?");
final AtomicReference<ResultSet> issueResultSet = new AtomicReference<>();
final Map<String, String> issue = new HashMap<>();

// Workflow step tests
final PreparedStatement selectWorkFlow = conn.prepareStatement("SELECT * FROM OS_CURRENTSTEP WHERE entry_id = ?");
final PreparedStatement selectWorkFlow = connection.prepareStatement("SELECT * FROM OS_CURRENTSTEP WHERE entry_id = ?");
final AtomicReference<ResultSet> wfResultSet = new AtomicReference<>();
final Map<String, String> workflow = new HashMap<>();

// Custom field field tests
final PreparedStatement selectCustomFieldValues = conn.prepareStatement("SELECT * FROM customfieldvalue WHERE ISSUE = ?");
final PreparedStatement selectCustomFieldValues = connection.prepareStatement("SELECT * FROM customfieldvalue WHERE ISSUE = ?");
final AtomicReference<ResultSet> customFieldResultSet = new AtomicReference<>();
final Map<String, String> customField = new HashMap<>();

// Comment tests
final PreparedStatement selectComment = conn.prepareStatement("SELECT * FROM jiraaction WHERE issueid = ?");
final PreparedStatement selectComment = connection.prepareStatement("SELECT * FROM jiraaction WHERE issueid = ?");
final AtomicReference<ResultSet> commentResultSet = new AtomicReference<>();
final Map<String, String> comment = new HashMap<>();

// Worklog tests
final PreparedStatement selectWorklog = conn.prepareStatement("SELECT * FROM worklog WHERE issueid = ?");
final PreparedStatement selectWorklog = connection.prepareStatement("SELECT * FROM worklog WHERE issueid = ?");
final AtomicReference<ResultSet> worklogResultSet = new AtomicReference<>();
final Map<String, String> worklog = new HashMap<>();

final List<TimedTestRunner> result = new ArrayList<>();

result.add(new TimedTestRunner(RETRIEVE_ISSUE, () -> {
selectIssue.setLong(1, ids.get(rnd.nextInt(issueCount)));
selectIssue.setLong(1, ids.get(random.nextInt(issueCount)));
issueResultSet.set(selectIssue.executeQuery());
return null;
}));
Expand Down Expand Up @@ -205,7 +211,7 @@ private List<TimedTestRunner> getTests() throws Exception {
System.err.println("No custom field values found");
return null;
}
if (!rs.next()){
if (!rs.next()) {
return null;
}
int columnCount = rs.getMetaData().getColumnCount();
Expand Down Expand Up @@ -239,7 +245,7 @@ private List<TimedTestRunner> getTests() throws Exception {
System.err.println("No comments found");
return null;
}
if (!rs.next()){
if (!rs.next()) {
return null;
}
int columnCount = rs.getMetaData().getColumnCount();
Expand Down Expand Up @@ -271,7 +277,7 @@ private List<TimedTestRunner> getTests() throws Exception {
System.err.println("No worklog found");
return null;
}
if (!rs.next()){
if (!rs.next()) {
return null;
}
int columnCount = rs.getMetaData().getColumnCount();
Expand All @@ -280,7 +286,7 @@ private List<TimedTestRunner> getTests() throws Exception {
}
return null;
}));

connection.close();
return result;
}
}

0 comments on commit 6bab2a5

Please sign in to comment.