Browse Source

Add debug logs to export of dry run H2 db

tags/3.7
Simon Brandhof 11 years ago
parent
commit
a8741b115d

+ 11
- 0
sonar-application/src/main/assembly/conf/logback.xml View File

@@ -76,10 +76,21 @@
<level value="WARN"/>
</logger>

<!-- Execution of measure filters -->
<logger name="org.sonar.MEASURE_FILTER">
<level value="WARN"/>
</logger>

<!-- Export of dry run database -->
<!--
<logger name="org.sonar.core.persistence.DbTemplate">
<level value="DEBUG"/>
</logger>
<logger name="org.sonar.core.persistence.DryRunDatabaseFactory">
<level value="DEBUG"/>
</logger>
-->

<root>
<level value="INFO"/>
<appender-ref ref="SONAR_FILE"/>

+ 7
- 9
sonar-core/src/main/java/org/sonar/core/persistence/DbTemplate.java View File

@@ -31,14 +31,7 @@ import org.sonar.api.ServerComponent;
import org.sonar.api.utils.SonarException;

import javax.sql.DataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.sql.Types;
import java.sql.*;
import java.util.List;

public class DbTemplate implements ServerComponent {
@@ -46,6 +39,7 @@ public class DbTemplate implements ServerComponent {

public DbTemplate copyTable(DataSource source, DataSource dest, String table, String... whereClauses) {
LOG.debug("Copy table {}", table);
long startup = System.currentTimeMillis();

String selectQuery = selectQuery(table, whereClauses);
truncate(dest, table);
@@ -56,6 +50,7 @@ public class DbTemplate implements ServerComponent {
Connection destConnection = null;
ResultSet destResultSet = null;
PreparedStatement destStatement = null;
int count = 0;
try {
sourceConnection = source.getConnection();
sourceStatement = sourceConnection.createStatement();
@@ -71,7 +66,7 @@ public class DbTemplate implements ServerComponent {
String insertSql = new StringBuilder().append("INSERT INTO ").append(table).append("(").append(Joiner.on(",").join(columnNames))
.append(") VALUES(").append(StringUtils.repeat("?", ",", columnNames.length)).append(")").toString();
destStatement = destConnection.prepareStatement(insertSql);
int count = 0;
do {
copyColumns(sourceResultSet, destStatement, columnNames, columnTypes);
count++;
@@ -86,6 +81,9 @@ public class DbTemplate implements ServerComponent {
destStatement.executeBatch();
destConnection.commit();
}
if (LOG.isDebugEnabled()) {
LOG.debug(" " + count + " rows of " + table + " copied in " + (System.currentTimeMillis() - startup) + " ms");
}
} catch (SQLException e) {
LOG.error("Fail to copy table " + table, e);
throw new IllegalStateException("Fail to copy table " + table, e);

+ 15
- 0
sonar-core/src/main/java/org/sonar/core/persistence/DryRunDatabaseFactory.java View File

@@ -21,6 +21,8 @@ package org.sonar.core.persistence;

import com.google.common.io.Files;
import org.apache.commons.dbcp.BasicDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.ServerComponent;
import org.sonar.api.issue.Issue;
import org.sonar.api.platform.ServerFileSystem;
@@ -33,6 +35,7 @@ import java.io.IOException;
import java.sql.SQLException;

public class DryRunDatabaseFactory implements ServerComponent {
private static final Logger LOG = LoggerFactory.getLogger(DryRunDatabaseFactory.class);
private static final String DIALECT = "h2";
private static final String DRIVER = "org.h2.Driver";
private static final String URL = "jdbc:h2:";
@@ -47,6 +50,7 @@ public class DryRunDatabaseFactory implements ServerComponent {
}

public byte[] createDatabaseForDryRun(@Nullable Long projectId) {
long startup = System.currentTimeMillis();
String name = serverFileSystem.getTempDir().getAbsolutePath() + "db-" + System.nanoTime();

try {
@@ -56,6 +60,17 @@ public class DryRunDatabaseFactory implements ServerComponent {
copy(source, destination, projectId);
close(destination);

if (LOG.isDebugEnabled()) {
File dbFile = new File(name + ".h2.db");
long size = dbFile.length();
long duration = System.currentTimeMillis() - startup;
if (projectId == null) {
LOG.debug("Dry Run Database created in " + duration + " ms, size is " + size + " bytes");
} else {
LOG.debug("Dry Run Database for project " + projectId + " created in " + duration + " ms, size is " + size + " bytes");
}
}

return dbFileContent(name);
} catch (SQLException e) {
throw new SonarException("Unable to create database for DryRun", e);

Loading…
Cancel
Save