Undo recent SQLite changes - might need to seek *another* DB engine - whats H2 like for concurrency?

This commit is contained in:
md_5 2013-07-06 08:30:38 +10:00
parent db4abfe486
commit 55c2bcd634

View File

@ -1,13 +1,10 @@
package net.md_5.bungee.reconnect; package net.md_5.bungee.reconnect;
import com.google.common.base.Throwables;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level; import java.util.logging.Level;
import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.config.ServerInfo;
@ -16,31 +13,14 @@ import net.md_5.bungee.api.connection.ProxiedPlayer;
public class SQLReconnectHandler extends AbstractReconnectManager public class SQLReconnectHandler extends AbstractReconnectManager
{ {
private final Set<Connection> allConnections = new HashSet<>(); private final Connection connection;
private final ThreadLocal<Connection> connectionPool = new ThreadLocal<Connection>()
{
@Override
protected Connection initialValue()
{
Connection con = null;
try
{
con = DriverManager.getConnection( "jdbc:sqlite:bungee.sqlite" );
} catch ( SQLException ex )
{
Throwables.propagate( ex );
}
allConnections.add( con );
return con;
}
};
public SQLReconnectHandler() throws ClassNotFoundException, SQLException public SQLReconnectHandler() throws ClassNotFoundException, SQLException
{ {
Class.forName( "org.sqlite.JDBC" ); Class.forName( "org.sqlite.JDBC" );
connection = DriverManager.getConnection( "jdbc:sqlite:bungee.sqlite" );
try ( PreparedStatement ps = connectionPool.get().prepareStatement( try ( PreparedStatement ps = connection.prepareStatement(
"CREATE TABLE IF NOT EXISTS players (" "CREATE TABLE IF NOT EXISTS players ("
+ "playerId INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT," + "playerId INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,"
+ "username TEXT NOT NULL UNIQUE COLLATE NOCASE," + "username TEXT NOT NULL UNIQUE COLLATE NOCASE,"
@ -56,7 +36,7 @@ public class SQLReconnectHandler extends AbstractReconnectManager
protected ServerInfo getStoredServer(ProxiedPlayer player) protected ServerInfo getStoredServer(ProxiedPlayer player)
{ {
String server = null; String server = null;
try ( PreparedStatement ps = connectionPool.get().prepareStatement( "SELECT server FROM players WHERE username = ?" ) ) try ( PreparedStatement ps = connection.prepareStatement( "SELECT server FROM players WHERE username = ?" ) )
{ {
ps.setString( 1, player.getName() ); ps.setString( 1, player.getName() );
try ( ResultSet rs = ps.executeQuery() ) try ( ResultSet rs = ps.executeQuery() )
@ -66,7 +46,7 @@ public class SQLReconnectHandler extends AbstractReconnectManager
server = rs.getString( 1 ); server = rs.getString( 1 );
} else } else
{ {
try ( PreparedStatement playerUpdate = connectionPool.get().prepareStatement( "INSERT INTO players( username ) VALUES( ? )" ) ) try ( PreparedStatement playerUpdate = connection.prepareStatement( "INSERT INTO players( username ) VALUES( ? )" ) )
{ {
playerUpdate.setString( 1, player.getName() ); playerUpdate.setString( 1, player.getName() );
playerUpdate.executeUpdate(); playerUpdate.executeUpdate();
@ -85,7 +65,7 @@ public class SQLReconnectHandler extends AbstractReconnectManager
public void setServer(ProxiedPlayer player) public void setServer(ProxiedPlayer player)
{ {
try ( PreparedStatement ps = connectionPool.get().prepareStatement( "UPDATE players SET server = ?, seen = ? WHERE username = ?" ) ) try ( PreparedStatement ps = connection.prepareStatement( "UPDATE players SET server = ?, seen = ? WHERE username = ?" ) )
{ {
ps.setString( 1, player.getServer().getInfo().getName() ); ps.setString( 1, player.getServer().getInfo().getName() );
ps.setInt( 2, (int) ( System.currentTimeMillis() / 1000L ) ); ps.setInt( 2, (int) ( System.currentTimeMillis() / 1000L ) );
@ -105,16 +85,12 @@ public class SQLReconnectHandler extends AbstractReconnectManager
@Override @Override
public void close() public void close()
{ {
for ( Connection con : allConnections ) try
{ {
try connection.close();
{ } catch ( SQLException ex )
con.close(); {
} catch ( SQLException ex ) ProxyServer.getInstance().getLogger().log( Level.WARNING, "Error closing SQLite connection", ex );
{
ProxyServer.getInstance().getLogger().log( Level.WARNING, "Error closing SQLite connection", ex );
}
} }
allConnections.clear();
} }
} }