From 0c5b4321efd5cc7b3851b452f141459085360960 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Mon, 22 May 2023 11:15:52 +0200 Subject: Update Database classes to match dmsd --- .../openslx/imagemaster/db/MysqlConnection.java | 68 ++++++++++++++-------- 1 file changed, 44 insertions(+), 24 deletions(-) (limited to 'src/main/java/org/openslx/imagemaster/db/MysqlConnection.java') diff --git a/src/main/java/org/openslx/imagemaster/db/MysqlConnection.java b/src/main/java/org/openslx/imagemaster/db/MysqlConnection.java index 443fce0..d9fe4f2 100644 --- a/src/main/java/org/openslx/imagemaster/db/MysqlConnection.java +++ b/src/main/java/org/openslx/imagemaster/db/MysqlConnection.java @@ -8,9 +8,10 @@ import java.util.List; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -public class MysqlConnection implements AutoCloseable { +public class MysqlConnection implements AutoCloseable +{ - private static final Logger LOGGER = LogManager.getLogger(MysqlConnection.class); + private static final Logger LOGGER = LogManager.getLogger( MysqlConnection.class ); private static final int CONNECTION_TIMEOUT_MS = 5 * 60 * 1000; @@ -22,62 +23,81 @@ public class MysqlConnection implements AutoCloseable { private List openStatements = new ArrayList<>(); - MysqlConnection(Connection rawConnection) { + MysqlConnection( Connection rawConnection ) + { this.rawConnection = rawConnection; } - - public MysqlStatement prepareStatement(String sql) throws SQLException { - return prepareStatement( sql, false ); - } - public MysqlStatement prepareStatement(String sql, boolean getKeys) throws SQLException { - if (!sql.startsWith("SELECT")) + public MysqlStatement prepareStatement( String sql ) throws SQLException + { + if ( !sql.startsWith( "SELECT" ) && !sql.startsWith( "DESCRIBE" ) && !sql.startsWith( "SHOW" ) ) { hasPendingQueries = true; - MysqlStatement statement = new MysqlStatement(rawConnection, sql, getKeys); - openStatements.add(statement); + } + MysqlStatement statement = new MysqlStatement( rawConnection, sql ); + openStatements.add( statement ); return statement; } - public void commit() throws SQLException { + public void commit() throws SQLException + { rawConnection.commit(); hasPendingQueries = false; } - public void rollback() throws SQLException { + public void rollback() throws SQLException + { rawConnection.rollback(); hasPendingQueries = false; } - boolean isValid() { + boolean isValid() + { return System.currentTimeMillis() < deadline; } @Override - public void close() { - if (hasPendingQueries) { - LOGGER.warn("Mysql connection had uncommited queries on .close()"); + public void close() + { + if ( hasPendingQueries ) { + LOGGER.warn( "Mysql connection had uncommited queries on .close()", + new RuntimeException( "Stack trace" ) ); + for ( MysqlStatement s : openStatements ) { + LOGGER.info( s.getQuery() ); + } hasPendingQueries = false; } try { rawConnection.rollback(); - } catch (SQLException e) { - LOGGER.warn("Rolling back uncommited queries failed!", e); + } catch ( SQLException e ) { + LOGGER.warn( "Rolling back uncommited queries failed!", e ); } - if (!openStatements.isEmpty()) { - for (MysqlStatement statement : openStatements) { + if ( !openStatements.isEmpty() ) { + for ( MysqlStatement statement : openStatements ) { statement.close(); } openStatements.clear(); } - Database.returnConnection(this); + try { + rawConnection.rollback(); + rawConnection.setAutoCommit( true ); + } catch ( SQLException e ) { + LOGGER.warn( "Rolling back uncommited queries failed!", e ); + } + Database.returnConnection( this ); } - void release() { + void release() + { try { rawConnection.close(); - } catch (SQLException e) { + } catch ( SQLException e ) { // Nothing meaningful to do } } + void setAutoCommit( boolean b ) throws SQLException + { + rawConnection.setAutoCommit( b ); + } + } -- cgit v1.2.3-55-g7522