1 package org.openslx.imagemaster.db;
3 import java.io.BufferedInputStream;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.IOException;
7 import java.sql.Connection;
8 import java.sql.DriverManager;
9 import java.sql.ResultSet;
10 import java.sql.SQLException;
11 import java.util.Collections;
12 import java.util.Properties;
13 import java.util.Queue;
15 import java.util.concurrent.ConcurrentHashMap;
16 import java.util.concurrent.ConcurrentLinkedQueue;
18 import org.apache.logging.log4j.LogManager;
19 import org.apache.logging.log4j.Logger;
29 private static final Queue<MysqlConnection>
pool =
new ConcurrentLinkedQueue<>();
34 private static final Set<MysqlConnection>
busyConnections = Collections.newSetFromMap(
new ConcurrentHashMap<MysqlConnection, Boolean>() );
36 private static final String
host;
38 private static final String
user;
49 Properties properties =
new Properties();
51 final BufferedInputStream stream =
new BufferedInputStream(
53 "config/mysql.properties" ) );
54 properties.load( stream );
56 }
catch ( FileNotFoundException e ) {
57 LOGGER.fatal(
"config/mysql.properties not found!" );
59 }
catch ( IOException e ) {
60 LOGGER.fatal(
"Error reading from config/mysql.properties: " + e.getMessage() );
62 }
catch ( Exception e ) {
63 LOGGER.fatal(
"Generic error loading mysql properties file." );
67 host = properties.getProperty(
"host" );
68 dbname = properties.getProperty(
"db" );
69 user = properties.getProperty(
"user" );
70 password = properties.getProperty(
"password" );
78 Class.forName(
"org.mariadb.jdbc.Driver" ).newInstance();
79 }
catch ( Exception e1 ) {
80 LOGGER.fatal(
"Cannot get mysql JDBC driver", e1 );
103 if ( !busyConnections.add( con ) )
104 throw new RuntimeException(
"Tried to hand out a busy connection!" );
109 }
catch ( SQLException e ) {
115 if ( busyConnections.size() > 20 ) {
116 LOGGER.warn(
"Too many open MySQL connections. Possible connection leak!" );
121 String uri =
"jdbc:mariadb://" + host +
"/" + dbname
122 +
"?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC"
123 +
"&characterSetResults=utf8&connectionCollation=utf8mb4_unicode_ci";
124 Connection rawConnection = DriverManager.getConnection( uri,
127 rawConnection.setAutoCommit(
false );
131 if ( !busyConnections.add( con ) )
132 throw new RuntimeException(
"Tried to hand out a busy connection!" );
134 }
catch ( SQLException e ) {
135 LOGGER.info(
"Failed to connect to local mysql server", e );
149 if ( !busyConnections.remove( connection ) )
150 throw new RuntimeException(
"Tried to return a mysql connection to the pool that was not taken!" );
151 pool.add( connection );
156 LOGGER.info(
"MySQL charset related variables:" );
158 MysqlStatement stmt = connection.prepareStatement(
"SHOW VARIABLES LIKE :what" );
161 while ( rs.next() ) {
162 LOGGER.info( rs.getString(
"Variable_name" ) +
": " + rs.getString(
"Value" ) );
166 while ( rs.next() ) {
167 LOGGER.info( rs.getString(
"Variable_name" ) +
": " + rs.getString(
"Value" ) );
169 }
catch ( SQLException e ) {
170 LOGGER.error(
"Query failed in Database.printCharsetInformation()", e );
172 LOGGER.info(
"End of variables" );
177 LOGGER.info(
"Available: " + pool.size() );
178 LOGGER.info(
"Busy: " + busyConnections.size() );
183 return e != null && e.getErrorCode() == 1062;
static boolean isDuplicateKeyException(SQLException e)
static final String dbname
static final String password
static final Set< MysqlConnection > busyConnections
Set of connections currently handed out.
static MysqlConnection getConnection()
Get a connection to the database.
ResultSet executeQuery()
Executes the statement, which must be a query.
static final Logger LOGGER
void setString(String name, String value)
Sets a parameter.
static void returnConnection(MysqlConnection connection)
Called by a MysqlConnection when its close()-method is called, so the connection will be added to the...
Class for creating PreparedStatements with named parameters.
void setAutoCommit(boolean b)
Some utilities to make our lives easier.
static void notNullFatal(Object something, String message)
Check if the given object is null, abort program if true.
static void printCharsetInformation()
static final Queue< MysqlConnection > pool
Pool of available connections.