Skip to content

Commit

Permalink
Merge pull request #13 from whimc/sql-pool
Browse files Browse the repository at this point in the history
Using connection pool
  • Loading branch information
Geph committed Jan 14, 2022
2 parents 450a5dc + 99fb34c commit b969cd3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 58 deletions.
9 changes: 8 additions & 1 deletion pom.xml
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>edu.whimc</groupId>
<artifactId>WHIMC-PositionTracker</artifactId>
<version>2.1.1</version>
<version>2.2.0</version>
<name>WHIMC Position Tracker</name>
<description>Track player positions to a database</description>

Expand All @@ -24,11 +24,18 @@
<version>1.13.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<!-- MySql Connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
</dependencies>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<!-- Repo for plugin -->
Expand Down
87 changes: 30 additions & 57 deletions src/main/java/edu/whimc/positiontracker/sql/MySQLConnection.java
@@ -1,18 +1,21 @@
package edu.whimc.positiontracker.sql;

import com.mysql.cj.jdbc.MysqlConnectionPoolDataSource;
import com.mysql.cj.jdbc.MysqlDataSource;
import com.mysql.cj.x.protobuf.MysqlxPrepare;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import edu.whimc.positiontracker.Tracker;
import org.bukkit.configuration.ConfigurationSection;

/**
* Handles the connection to the SQL database.
*/
public class MySQLConnection {
/** The template for the URL. */
public static final String URL_TEMPLATE = "jdbc:mysql://%s:%s/%s";

/** The SQL command to create a table. */
public static final String CREATE_TABLE =
"CREATE TABLE IF NOT EXISTS `whimc_player_positions` (" +
Expand All @@ -27,85 +30,55 @@ public class MySQLConnection {
" `time` BIGINT NOT NULL," +
" PRIMARY KEY (`rowid`));";

/** The connection to the SQL database. */
private Connection connection;
/** The SQL database credentials. */
private String host, database, username, password, url;
/** The SQL database port. */
private int port;
private MysqlDataSource dataSource;

/**
* Constructs a MySQLConnection.
*
* @param plugin The instance of the plugin.
*/
public MySQLConnection(Tracker plugin) {
// fetch credentials and database info from config
this.host = plugin.getConfig().getString("mysql.host", "localhost");
this.port = plugin.getConfig().getInt("mysql.port", 3306);
this.database = plugin.getConfig().getString("mysql.database", "minecraft");
this.username = plugin.getConfig().getString("mysql.username", "user");
this.password = plugin.getConfig().getString("mysql.password", "pass");
this.dataSource = new MysqlConnectionPoolDataSource();

// create the URL with the fetched information
this.url = String.format(URL_TEMPLATE, host, port, database);
ConfigurationSection config = plugin.getConfig();
this.dataSource.setServerName(config.getString("mysql.host", "localhost"));
this.dataSource.setPortNumber(config.getInt("mysql.port", 3306));
this.dataSource.setDatabaseName(config.getString("mysql.database", "minecraft"));
this.dataSource.setUser(config.getString("mysql.username", "user"));
this.dataSource.setPassword(config.getString("mysql.password", "pass"));
}

/**
* Initializes the MySQLConnection.
* Attempt to connect to the database and create/update the schema.
*
* @return true if the connection succeeds.
* @return Whether the database was successfully initialized.
*/
public boolean initialize() {
// ensure there is a connection
if (getConnection() == null) {
return false;
}

try {
// create a table
PreparedStatement statement = this.connection.prepareStatement(CREATE_TABLE);
statement.execute();
} catch (SQLException e) {
try (Connection connection = getConnection()) {
if (connection == null) {
return false;
}

try (PreparedStatement statement = connection.prepareStatement(CREATE_TABLE)) {
statement.execute();
}
} catch (SQLException unused) {
return false;
}

return true;
}

/**
* Fetches the connection to the SQL database.
*
* @return the SQL database Connection.
* A connection to the configured database or null if unsuccessful.
*/
public Connection getConnection() {
try {
// ensure connection exists and is open
if (this.connection != null && !this.connection.isClosed()) {
return this.connection;
}

// try connecting if a connection doesn't currently exist
this.connection = DriverManager.getConnection(this.url, this.username, this.password);
} catch (SQLException ignored) {
public Connection getConnection() throws SQLException {
Connection connection = this.dataSource.getConnection();
if (!connection.isValid(1)) {
return null;
}

return this.connection;
}

/**
* Closes the connection to the SQL database.
*/
public void closeConnection() {
// ensure connection exists
if (this.connection != null) {
try {
this.connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return connection;
}

}

0 comments on commit b969cd3

Please sign in to comment.