bwLehrpool Masterserver
Manages authentication and sharing of virtual machines between participating institutions
MysqlConnection.java
Go to the documentation of this file.
1 package org.openslx.imagemaster.db;
2 
3 import java.sql.Connection;
4 import java.sql.SQLException;
5 import java.util.ArrayList;
6 import java.util.List;
7 
8 import org.apache.logging.log4j.LogManager;
9 import org.apache.logging.log4j.Logger;
10 
11 public class MysqlConnection implements AutoCloseable
12 {
13 
14  private static final Logger LOGGER = LogManager.getLogger( MysqlConnection.class );
15 
16  private static final int CONNECTION_TIMEOUT_MS = 5 * 60 * 1000;
17 
18  private final long deadline = System.currentTimeMillis() + CONNECTION_TIMEOUT_MS;
19 
20  private final Connection rawConnection;
21 
22  private boolean hasPendingQueries = false;
23 
24  private List<MysqlStatement> openStatements = new ArrayList<>();
25 
26  MysqlConnection( Connection rawConnection )
27  {
28  this.rawConnection = rawConnection;
29  }
30 
31  public MysqlStatement prepareStatement( String sql ) throws SQLException
32  {
33  if ( !sql.startsWith( "SELECT" ) && !sql.startsWith( "DESCRIBE" ) && !sql.startsWith( "SHOW" ) ) {
34  hasPendingQueries = true;
35  }
36  MysqlStatement statement = new MysqlStatement( rawConnection, sql );
37  openStatements.add( statement );
38  return statement;
39  }
40 
41  public void commit() throws SQLException
42  {
43  rawConnection.commit();
44  hasPendingQueries = false;
45  }
46 
47  public void rollback() throws SQLException
48  {
49  rawConnection.rollback();
50  hasPendingQueries = false;
51  }
52 
53  boolean isValid()
54  {
55  return System.currentTimeMillis() < deadline;
56  }
57 
58  @Override
59  public void close()
60  {
61  if ( hasPendingQueries ) {
62  LOGGER.warn( "Mysql connection had uncommited queries on .close()",
63  new RuntimeException( "Stack trace" ) );
64  for ( MysqlStatement s : openStatements ) {
65  LOGGER.info( s.getQuery() );
66  }
67  hasPendingQueries = false;
68  }
69  try {
70  rawConnection.rollback();
71  } catch ( SQLException e ) {
72  LOGGER.warn( "Rolling back uncommited queries failed!", e );
73  }
74  if ( !openStatements.isEmpty() ) {
75  for ( MysqlStatement statement : openStatements ) {
76  statement.close();
77  }
78  openStatements.clear();
79  }
80  try {
81  rawConnection.rollback();
82  rawConnection.setAutoCommit( true );
83  } catch ( SQLException e ) {
84  LOGGER.warn( "Rolling back uncommited queries failed!", e );
85  }
86  Database.returnConnection( this );
87  }
88 
89  void release()
90  {
91  try {
92  rawConnection.close();
93  } catch ( SQLException e ) {
94  // Nothing meaningful to do
95  }
96  }
97 
98  void setAutoCommit( boolean b ) throws SQLException
99  {
100  rawConnection.setAutoCommit( b );
101  }
102 
103 }
static void returnConnection(MysqlConnection connection)
Called by a MysqlConnection when its close()-method is called, so the connection will be added to the...
Definition: Database.java:147
Class for creating PreparedStatements with named parameters.
MysqlStatement prepareStatement(String sql)