Removed DBInitException throws clauses from methods

This commit is contained in:
Rsl1122 2019-02-18 18:21:14 +02:00
parent 0b7b0a5a16
commit acae1c4b3d
12 changed files with 42 additions and 45 deletions

View File

@ -78,16 +78,16 @@ public class ShutdownHook extends Thread {
Map<UUID, Session> activeSessions = SessionCache.getActiveSessions();
prepareSessionsForStorage(activeSessions, System.currentTimeMillis());
saveActiveSessions(activeSessions);
} catch (IllegalStateException ignored) {
/* Database is not initialized */
} catch (DBInitException e) {
errorHandler.log(L.ERROR, this.getClass(), e);
} catch (IllegalStateException ignored) {
/* Database is not initialized */
} finally {
closeDatabase(dbSystem.getDatabase());
}
}
private void saveActiveSessions(Map<UUID, Session> activeSessions) throws DBInitException {
private void saveActiveSessions(Map<UUID, Session> activeSessions) {
Database database = dbSystem.getDatabase();
if (database.getState() == Database.State.CLOSED) {
// Ensure that database is not closed when performing the transaction.

View File

@ -29,7 +29,14 @@ import java.util.concurrent.Future;
*/
public interface Database {
void init() throws DBInitException;
/**
* Initializes the Database.
* <p>
* Queries can be performed after this request has completed all required transactions for the database operations.
*
* @throws DBInitException if Database fails to initiate.
*/
void init();
void close();

View File

@ -68,7 +68,7 @@ public class H2DB extends SQLDB {
}
@Override
public void setupDataSource() throws DBInitException {
public void setupDataSource() {
try {
connection = getNewConnection(databaseFile);
} catch (SQLException e) {

View File

@ -79,7 +79,7 @@ public class MySQLDB extends SQLDB {
* Setups the {@link HikariDataSource}
*/
@Override
public void setupDataSource() throws DBInitException {
public void setupDataSource() {
try {
HikariConfig hikariConfig = new HikariConfig();

View File

@ -89,18 +89,8 @@ public abstract class SQLDB extends AbstractDatabase {
this.transactionExecutorServiceProvider = () -> Executors.newSingleThreadExecutor(new ThreadFactoryBuilder().setNameFormat("Plan " + getClass().getSimpleName() + "-transaction-thread-%d").build());
}
/**
* Initializes the Database.
* <p>
* All tables exist in the database after call to this.
* Updates Schema to latest version.
* Converts Unsaved Bukkit player files to database data.
* Cleans the database.
*
* @throws DBInitException if Database fails to initiate.
*/
@Override
public void init() throws DBInitException {
public void init() {
List<Runnable> unfinishedTransactions = closeTransactionExecutor(transactionExecutor);
this.transactionExecutor = transactionExecutorServiceProvider.get();
@ -215,7 +205,12 @@ public abstract class SQLDB extends AbstractDatabase {
}
}
public abstract void setupDataSource() throws DBInitException;
/**
* Set up the source for connections.
*
* @throws DBInitException If the DataSource fails to be initialized.
*/
public abstract void setupDataSource();
@Override
public void close() {

View File

@ -67,7 +67,7 @@ public class SQLiteDB extends SQLDB {
}
@Override
public void setupDataSource() throws DBInitException {
public void setupDataSource() {
try {
connection = getNewConnection(databaseFile);
} catch (SQLException e) {

View File

@ -16,7 +16,6 @@
*/
package com.djrapitops.plan.db;
import com.djrapitops.plan.api.exceptions.database.DBInitException;
import com.djrapitops.plan.data.WebUser;
import com.djrapitops.plan.data.container.*;
import com.djrapitops.plan.data.store.Key;
@ -154,13 +153,13 @@ public abstract class CommonDBTest {
});
}
public void commitTest() throws DBInitException {
public void commitTest() {
db.close();
db.init();
}
@Test
public void testSaveCommandUse() throws DBInitException {
public void testSaveCommandUse() {
Map<String, Integer> expected = new HashMap<>();
expected.put("plan", 1);
@ -181,7 +180,7 @@ public abstract class CommonDBTest {
}
@Test
public void commandUsageSavingDoesNotCreateNewEntriesForOldCommands() throws DBInitException {
public void commandUsageSavingDoesNotCreateNewEntriesForOldCommands() {
Map<String, Integer> expected = new HashMap<>();
expected.put("plan", 1);
@ -238,7 +237,7 @@ public abstract class CommonDBTest {
}
@Test
public void geoInformationIsStored() throws DBInitException, NoSuchAlgorithmException {
public void geoInformationIsStored() throws NoSuchAlgorithmException {
saveUserOne();
String expectedIP = "1.2.3.4";
@ -256,7 +255,7 @@ public abstract class CommonDBTest {
}
@Test
public void testNicknamesTable() throws DBInitException {
public void testNicknamesTable() {
saveUserOne();
Nickname expected = new Nickname("TestNickname", System.currentTimeMillis(), serverUUID);
@ -270,7 +269,7 @@ public abstract class CommonDBTest {
}
@Test
public void webUserIsRegistered() throws DBInitException {
public void webUserIsRegistered() {
WebUser expected = new WebUser(TestConstants.PLAYER_ONE_NAME, "RandomGarbageBlah", 0);
db.executeTransaction(new RegisterWebUserTransaction(expected));
commitTest();
@ -281,20 +280,20 @@ public abstract class CommonDBTest {
}
@Test
public void multipleWebUsersAreFetchedAppropriately() throws DBInitException {
public void multipleWebUsersAreFetchedAppropriately() {
webUserIsRegistered();
assertEquals(1, db.query(WebUserQueries.fetchAllPlanWebUsers()).size());
}
@Test
public void webUserIsRemoved() throws DBInitException {
public void webUserIsRemoved() {
webUserIsRegistered();
db.executeTransaction(new RemoveWebUserTransaction(TestConstants.PLAYER_ONE_NAME));
assertFalse(db.query(WebUserQueries.fetchWebUser(TestConstants.PLAYER_ONE_NAME)).isPresent());
}
@Test
public void worldNamesAreStored() throws DBInitException {
public void worldNamesAreStored() {
String[] expected = {"Test", "Test2", "Test3"};
saveWorlds(expected);
@ -343,7 +342,7 @@ public abstract class CommonDBTest {
}
@Test
public void testSessionPlaytimeSaving() throws DBInitException {
public void testSessionPlaytimeSaving() {
saveTwoWorlds();
saveUserOne();
saveUserTwo();
@ -374,7 +373,7 @@ public abstract class CommonDBTest {
}
@Test
public void testSessionSaving() throws DBInitException {
public void testSessionSaving() {
saveUserOne();
saveUserTwo();
@ -432,7 +431,7 @@ public abstract class CommonDBTest {
}
@Test
public void playerNameIsUpdatedWhenPlayerLogsIn() throws DBInitException {
public void playerNameIsUpdatedWhenPlayerLogsIn() {
saveUserOne();
OptionalAssert.equals(playerUUID, db.query(UserIdentifierQueries.fetchPlayerUUIDOf(TestConstants.PLAYER_ONE_NAME)));
@ -447,7 +446,7 @@ public abstract class CommonDBTest {
}
@Test
public void testUsersTableKickSaving() throws DBInitException {
public void testUsersTableKickSaving() {
saveUserOne();
OptionalAssert.equals(1, db.query(BaseUserQueries.fetchBaseUserOfPlayer(playerUUID)).map(BaseUser::getTimesKicked));
@ -568,7 +567,7 @@ public abstract class CommonDBTest {
}
@Test
public void testSessionTableGetInfoOfServer() throws DBInitException {
public void testSessionTableGetInfoOfServer() {
saveUserOne();
saveUserTwo();
@ -606,7 +605,7 @@ public abstract class CommonDBTest {
}
@Test
public void testKillTableGetKillsOfServer() throws DBInitException {
public void testKillTableGetKillsOfServer() {
saveUserOne();
saveUserTwo();

View File

@ -16,7 +16,6 @@
*/
package com.djrapitops.plan.db;
import com.djrapitops.plan.api.exceptions.database.DBInitException;
import com.djrapitops.plan.data.store.containers.ServerContainer;
import com.djrapitops.plan.data.store.keys.ServerKeys;
import com.djrapitops.plan.db.access.queries.containers.ContainerFetchQueries;
@ -66,7 +65,7 @@ public class DBPatchH2RegressionTest extends DBPatchRegressionTest {
private H2DB underTest;
@Before
public void setUpDBWithOldSchema() throws DBInitException {
public void setUpDBWithOldSchema() {
PlanConfig config = component.getPlanSystem().getConfigSystem().getConfig();
config.set(DatabaseSettings.MYSQL_USER, "user");

View File

@ -17,7 +17,6 @@
package com.djrapitops.plan.db;
import com.djrapitops.plan.api.exceptions.EnableException;
import com.djrapitops.plan.api.exceptions.database.DBInitException;
import com.djrapitops.plan.data.store.containers.ServerContainer;
import com.djrapitops.plan.data.store.keys.ServerKeys;
import com.djrapitops.plan.db.access.queries.containers.ContainerFetchQueries;
@ -78,7 +77,7 @@ public class DBPatchMySQLRegressionTest extends DBPatchRegressionTest {
}
@Before
public void setUpDBWithOldSchema() throws DBInitException, EnableException {
public void setUpDBWithOldSchema() throws EnableException {
PlanSystem system = component.getPlanSystem();
PlanConfig config = system.getConfigSystem().getConfig();
config.set(DatabaseSettings.MYSQL_DATABASE, "Plan");

View File

@ -16,7 +16,6 @@
*/
package com.djrapitops.plan.db;
import com.djrapitops.plan.api.exceptions.database.DBInitException;
import com.djrapitops.plan.data.store.containers.ServerContainer;
import com.djrapitops.plan.data.store.keys.ServerKeys;
import com.djrapitops.plan.db.access.queries.containers.ContainerFetchQueries;
@ -64,7 +63,7 @@ public class DBPatchSQLiteRegressionTest extends DBPatchRegressionTest {
private SQLiteDB underTest;
@Before
public void setUpDBWithOldSchema() throws DBInitException {
public void setUpDBWithOldSchema() {
underTest = component.getPlanSystem().getDatabaseSystem().getSqLiteFactory()
.usingFileCalled("test");
underTest.setTransactionExecutorServiceProvider(MoreExecutors::newDirectExecutorService);

View File

@ -16,7 +16,6 @@
*/
package com.djrapitops.plan.db;
import com.djrapitops.plan.api.exceptions.database.DBInitException;
import com.djrapitops.plan.data.container.GeoInfo;
import com.djrapitops.plan.db.access.queries.ServerAggregateQueries;
import com.djrapitops.plan.db.access.queries.objects.ServerQueries;
@ -58,7 +57,7 @@ public class SQLiteTest extends CommonDBTest {
}
@Test
public void testServerTableBungeeSave() throws DBInitException {
public void testServerTableBungeeSave() {
Optional<Server> bungeeInfo = db.query(ServerQueries.fetchProxyServerInformation());
assertFalse(bungeeInfo.isPresent());
@ -79,7 +78,7 @@ public class SQLiteTest extends CommonDBTest {
}
@Test
public void testServerTableBungee() throws DBInitException {
public void testServerTableBungee() {
testServerTableBungeeSave();
Map<UUID, Server> serverInformation = db.query(ServerQueries.fetchPlanServerInformation());

View File

@ -57,7 +57,7 @@ public class SpongeMySQLDB extends MySQLDB {
}
@Override
public void setupDataSource() throws DBInitException {
public void setupDataSource() {
Optional<SqlService> sqlServiceProvider = Sponge.getServiceManager().provide(SqlService.class);
if (!sqlServiceProvider.isPresent()) {
return;