Skip to content

Commit

Permalink
More cleanup and added DAO authors from deleted interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
neon-dev committed Jun 15, 2024
1 parent ce23cdc commit 4cf9e68
Show file tree
Hide file tree
Showing 40 changed files with 535 additions and 930 deletions.
147 changes: 4 additions & 143 deletions commons/src/com/aionemu/commons/database/DatabaseFactory.java
Original file line number Diff line number Diff line change
@@ -1,61 +1,30 @@
package com.aionemu.commons.database;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aionemu.commons.configs.DatabaseConfig;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import com.zaxxer.hikari.pool.HikariPool;

/**
* <b>Database Factory</b><br>
* <br>
* This file is used for creating a pool of connections for the server.<br>
* It utilizes database.properties and creates a pool of connections and automatically recycles them when closed.<br>
* <br>
* DB.java utilizes the class.<br>
* <br>
* <p/>
*
* @author Disturbing, SoulKeeper
*/
public class DatabaseFactory {

/**
* Logger for this class
*/
private static final Logger log = LoggerFactory.getLogger(DatabaseFactory.class);

/**
* Connection Pool holds all connections - Idle or Active
*/
private static DataSource dataSource;

/**
* Returns name of the database that is used For isntance, MySQL returns "MySQL"
*/
private static String databaseName;

/**
* Retursn major version that is used For instance, MySQL 5.0.51 community edition returns 5
*/
private static int databaseMajorVersion;

/**
* Retursn minor version that is used For instance, MySQL 5.0.51 community edition returns 0
*/
private static int databaseMinorVersion;

/**
* Initializes DatabaseFactory.
*/
public synchronized static void init() {
if (dataSource != null) {
return;
Expand All @@ -69,131 +38,23 @@ public synchronized static void init() {
config.setConnectionTimeout(DatabaseConfig.DATABASE_TIMEOUT);

dataSource = new HikariDataSource(config);

try (Connection c = getConnection()) {
DatabaseMetaData dmd = c.getMetaData();
databaseName = dmd.getDatabaseProductName();
databaseMajorVersion = dmd.getDatabaseMajorVersion();
databaseMinorVersion = dmd.getDatabaseMinorVersion();
} catch (Exception e) {
throw new Error("Could not connect to " + DatabaseConfig.DATABASE_URL, e);
}
}

/**
* Returns an active connection from pool. This function utilizes the dataSource which grabs an object from the ObjectPool within its limits. The
* GenericObjectPool.borrowObject()' function utilized in 'DataSource.getConnection()' does not allow any connections to be returned as null, thus a
* null check is not needed. Throws SQLException in case of a Failed Connection
*
* @return Connection pooled connection
* @throws java.sql.SQLException
* if can't get connection
* @return An active connection from the {@link HikariPool connection pool}
* @see HikariDataSource#getConnection()
*/
public static Connection getConnection() throws SQLException {
Connection con = dataSource.getConnection();

if (!con.getAutoCommit()) {
log.error("Connection Settings Error: Connection obtained from database factory should be in auto-commit"
+ " mode. Forcing auto-commit to true. Please check source code for connections beeing not properly closed.");
LoggerFactory.getLogger(DatabaseFactory.class).error("Connection was not in auto-commit mode.", new IllegalStateException());
con.setAutoCommit(true);
}

return con;
}

/**
* Closes both prepared statement and result set
*
* @param st
* prepared statement to close
* @param con
* connection to close
*/
public static void close(PreparedStatement st, Connection con) {
close(st);
close(con);
}

/**
* Helper method for silently close PreparedStament object.<br>
* Associated connection object will not be closed.
*
* @param st
* prepared statement to close
*/
public static void close(PreparedStatement st) {
if (st == null) {
return;
}

try {
if (!st.isClosed()) {
st.close();
}
} catch (SQLException e) {
log.error("Can't close Prepared Statement", e);
}
}

/**
* Closes connection and returns it to the pool.<br>
* It's ok to pass null variable here.<br>
* When closing connection - this method will make sure that connection returned to the pool in in autocommit mode.<br>
* . If it's not - autocommit mode will be forced to 'true'
*
* @param con
* Connection object to close, can be null
*/
public static void close(Connection con) {
if (con == null)
return;

try {
if (!con.getAutoCommit()) {
con.setAutoCommit(true);
}
} catch (SQLException e) {
log.error("Failed to set autocommit to true while closing connection: ", e);
}

try {
con.close();
} catch (SQLException e) {
log.error("DatabaseFactory: Failed to close database connection!", e);
}
}

/**
* Returns database name. For instance MySQL 5.0.51 community edition returns MySQL
*
* @return database name that is used.
*/
public static String getDatabaseName() {
return databaseName;
}

/**
* Returns database version. For instance MySQL 5.0.51 community edition returns 5
*
* @return database major version
*/
public static int getDatabaseMajorVersion() {
return databaseMajorVersion;
}

/**
* Returns database minor version. For instance MySQL 5.0.51 community edition reutnrs 0
*
* @return database minor version
*/
public static int getDatabaseMinorVersion() {
return databaseMinorVersion;
}

/**
* Default constructor.
*/
private DatabaseFactory() {
//
}
}
2 changes: 0 additions & 2 deletions game-server/data/handlers/admincommands/Headhunting.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@
import com.aionemu.gameserver.utils.chathandlers.AdminCommand;

/**
* Created on 01.06.2016
* A command that handles analysis and rewarding for seasonal head hunting events.
* Take care when using the reward parameter, it will clean all references including the database tables.
*
* @author Estrayl
* @since AION 4.8
*/
public class Headhunting extends AdminCommand {

Expand Down
4 changes: 3 additions & 1 deletion game-server/sql/update.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@

ALTER TABLE `old_names`
ADD COLUMN `renamed_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP() AFTER `new_name`,
ADD KEY `renamed_date` (`renamed_date`);
ADD KEY `renamed_date` (`renamed_date`);

DROP TABLE `player_vars`;
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import com.aionemu.gameserver.utils.stats.AbyssRankEnum;

/**
* @author ATracer
* @author ATracer, Divinity, nrg
*/
public class AbyssRankDAO {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import com.aionemu.gameserver.model.gameobjects.Persistable.PersistentState;

/**
* @author Luzien
* @author ViAl, Luzien
*/
public class AccountPassportsDAO {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

/**
* DAO that manages Announcements
*
*
* @author Divinity
*/
public class AnnouncementsDAO {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

/**
* Responsible for saving and loading data on players' block lists
*
*
* @author Ben
*/
public class BlockListDAO {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.aionemu.commons.database.DatabaseFactory;

/**
* @author Estrayl
* @author Estrayl, Neon
*/
public class BonusPackDAO {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import com.aionemu.gameserver.model.Race;

/**
* @author Jo
* @author Jo, Estrayl
*/
public class CustomInstanceDAO {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import com.aionemu.gameserver.model.gameobjects.Persistable.PersistentState;

/**
* @author Jo
* @author Jo, Estrayl
*/
public class CustomInstancePlayerModelEntryDAO {

Expand Down
60 changes: 26 additions & 34 deletions game-server/src/com/aionemu/gameserver/dao/GuideDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ public class GuideDAO {
public static final String SELECT_GUIDE_QUERY = "SELECT * FROM `guides` WHERE `guide_id`=? AND `player_id`=?";

public static boolean deleteGuide(int guide_id) {
try {
try (Connection con = DatabaseFactory.getConnection(); PreparedStatement stmt = con.prepareStatement(DELETE_QUERY)) {
stmt.setInt(1, guide_id);
stmt.execute();
}
try (Connection con = DatabaseFactory.getConnection(); PreparedStatement stmt = con.prepareStatement(DELETE_QUERY)) {
stmt.setInt(1, guide_id);
stmt.execute();
} catch (Exception e) {
log.error("Error delete guide_id: " + guide_id, e);
return false;
Expand All @@ -39,18 +37,16 @@ public static boolean deleteGuide(int guide_id) {
}

public static List<Guide> loadGuides(int playerId) {
final List<Guide> guides = new ArrayList<>();
try {
try (Connection con = DatabaseFactory.getConnection(); PreparedStatement stmt = con.prepareStatement(SELECT_QUERY)) {
stmt.setInt(1, playerId);
try (ResultSet rset = stmt.executeQuery()) {
while (rset.next()) {
int guide_id = rset.getInt("guide_id");
int player_id = rset.getInt("player_id");
String title = rset.getString("title");
Guide guide = new Guide(guide_id, player_id, title);
guides.add(guide);
}
List<Guide> guides = new ArrayList<>();
try (Connection con = DatabaseFactory.getConnection(); PreparedStatement stmt = con.prepareStatement(SELECT_QUERY)) {
stmt.setInt(1, playerId);
try (ResultSet rset = stmt.executeQuery()) {
while (rset.next()) {
int guide_id = rset.getInt("guide_id");
int player_id = rset.getInt("player_id");
String title = rset.getString("title");
Guide guide = new Guide(guide_id, player_id, title);
guides.add(guide);
}
}
} catch (Exception e) {
Expand All @@ -61,15 +57,13 @@ public static List<Guide> loadGuides(int playerId) {

public static Guide loadGuide(int player_id, int guide_id) {
Guide guide = null;
try {
try (Connection con = DatabaseFactory.getConnection(); PreparedStatement stmt = con.prepareStatement(SELECT_GUIDE_QUERY)) {
stmt.setInt(1, guide_id);
stmt.setInt(2, player_id);
try (ResultSet rset = stmt.executeQuery()) {
while (rset.next()) {
String title = rset.getString("title");
guide = new Guide(guide_id, player_id, title);
}
try (Connection con = DatabaseFactory.getConnection(); PreparedStatement stmt = con.prepareStatement(SELECT_GUIDE_QUERY)) {
stmt.setInt(1, guide_id);
stmt.setInt(2, player_id);
try (ResultSet rset = stmt.executeQuery()) {
while (rset.next()) {
String title = rset.getString("title");
guide = new Guide(guide_id, player_id, title);
}
}
} catch (Exception e) {
Expand All @@ -79,14 +73,12 @@ public static Guide loadGuide(int player_id, int guide_id) {
}

public static void saveGuide(int guide_id, Player player, String title) {
try {
try (Connection con = DatabaseFactory.getConnection();
PreparedStatement stmt = con.prepareStatement("INSERT INTO guides(guide_id, title, player_id)" + "VALUES (?, ?, ?)")) {
stmt.setInt(1, guide_id);
stmt.setString(2, title);
stmt.setInt(3, player.getObjectId());
stmt.execute();
}
try (Connection con = DatabaseFactory.getConnection();
PreparedStatement stmt = con.prepareStatement("INSERT INTO guides(guide_id, title, player_id)" + "VALUES (?, ?, ?)")) {
stmt.setInt(1, guide_id);
stmt.setString(2, title);
stmt.setInt(3, player.getObjectId());
stmt.execute();
} catch (Exception e) {
log.error("Error saving playerName: " + player, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
import com.aionemu.gameserver.services.PvpService;

/**
* Created on 30.05.2016
*
* @author Estrayl
* @since AION 4.8
*/
public class HeadhuntingDAO {

Expand Down
Loading

0 comments on commit 4cf9e68

Please sign in to comment.