Added connection re-establishment to database calls if error occurs.

This commit is contained in:
Jonathan Williams 2015-02-14 22:50:52 -08:00
parent c129deb201
commit fd0547d9d3
4 changed files with 68 additions and 12 deletions

View File

@ -62,12 +62,17 @@ public abstract class RepositoryBase implements Listener
protected abstract void update();
protected Connection getConnection()
{
return getConnection(false);
}
protected Connection getConnection(boolean validate)
{
synchronized (_connectionLock)
{
try
{
if (_connection == null)
if (_connection == null || (validate && !_connection.isValid(2)))
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
}
catch (SQLException e)
@ -109,7 +114,18 @@ public abstract class RepositoryBase implements Listener
}
catch (SQLException exception)
{
// exception.getSQLState()
try
{
if (!_connection.isValid(5))
{
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
executeInsert(query, callable, columns);
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
catch (Exception exception)
{
@ -148,6 +164,21 @@ public abstract class RepositoryBase implements Listener
callable.processResultSet(resultSet);
}
catch (SQLException exception)
{
try
{
if (!_connection.isValid(5))
{
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
executeQuery(statement, callable, columns);
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
catch (Exception exception)
{
exception.printStackTrace();
@ -181,6 +212,21 @@ public abstract class RepositoryBase implements Listener
executeQuery(preparedStatement, callable, columns);
}
catch (SQLException exception)
{
try
{
if (!_connection.isValid(5))
{
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
executeQuery(query, callable, columns);
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
catch (Exception exception)
{
exception.printStackTrace();
@ -214,6 +260,21 @@ public abstract class RepositoryBase implements Listener
affectedRows = preparedStatement.executeUpdate();
}
catch (SQLException exception)
{
try
{
if (!_connection.isValid(5))
{
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
executeUpdate(preparedStatement, columns);
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
catch (Exception exception)
{
exception.printStackTrace();

View File

@ -61,14 +61,9 @@ public class MessageManager extends MiniClientPlugin<ClientMessage>
_friendsManager = friendManager;
_serverName = GetPlugin().getConfig().getString("serverstatus.name");
ServerCommandManager.getInstance().registerCommandType("AnnouncementCommand", AnnouncementCommand.class,
new AnnouncementHandler());
ServerCommandManager.getInstance().registerCommandType("AnnouncementCommand", AnnouncementCommand.class, new AnnouncementHandler());
MessageHandler messageHandler = new MessageHandler(this);
ServerCommandManager.getInstance()
.registerCommandType("RedisMessageCallback", RedisMessageCallback.class, messageHandler);
ServerCommandManager.getInstance().registerCommandType("RedisMessage", RedisMessage.class, messageHandler);
ServerCommandManager.getInstance().registerCommandType("RedisMessage", RedisMessage.class, new MessageHandler(this));
}
public void AddCommands()
@ -396,7 +391,6 @@ public class MessageManager extends MiniClientPlugin<ClientMessage>
public void sendMessage(final Player sender, String target, String message, boolean isReply, final boolean adminMessage)
{
FriendData friends = _friendsManager.Get(sender);
FriendStatus friend = null;

View File

@ -79,7 +79,8 @@ public class StatsRepository extends RepositoryBase
{
try
{
DSLContext context = DSL.using(getConnection());
DSLContext context = DSL.using(getConnection(true));
List<Update> updates = new ArrayList<>();
List<Insert> inserts = new ArrayList<>();

View File

@ -78,7 +78,7 @@ public class Clans extends JavaPlugin
BlockRestore blockRestore = new BlockRestore(this);
IgnoreManager ignoreManager = new IgnoreManager(this, _clientManager, preferenceManager, portal);
new MessageManager(this, _clientManager, preferenceManager, ignoreManager, punish);
new MessageManager(this, _clientManager, preferenceManager, ignoreManager, punish, new FriendManager(this, _clientManager, preferenceManager, portal));
new MemoryFix(this);
new Explosion(this, blockRestore);