@@ -25,10 +25,8 @@ package eu.thesimplecloud.base.manager.database
2525import eu.thesimplecloud.api.player.IOfflineCloudPlayer
2626import eu.thesimplecloud.api.player.OfflineCloudPlayer
2727import eu.thesimplecloud.jsonlib.JsonLib
28- import eu.thesimplecloud.launcher.startup.Launcher
2928import java.sql.*
3029import java.util.*
31- import java.util.concurrent.TimeUnit
3230
3331
3432/* *
@@ -40,42 +38,36 @@ import java.util.concurrent.TimeUnit
4038class SQLOfflineCloudPlayerHandler (private val databaseConnectionInformation : DatabaseConnectionInformation ) :
4139 AbstractOfflineCloudPlayerHandler () {
4240
43- var connection: Connection ? = null
44- private set
45-
4641 private val playerCollectionName = databaseConnectionInformation.collectionPrefix + " players"
4742
4843 init {
49- runReconnectLoop()
5044 createDatabaseAndIndicesIfNotExist()
5145 }
5246
5347 private fun createDatabaseAndIndicesIfNotExist () {
5448 if (! doesTableExist()) {
55- val statement =
56- connection!! .prepareStatement(" CREATE TABLE IF NOT EXISTS `$playerCollectionName ` (`uniqueId` varchar(36), `name` varchar(16), `data` LONGBLOB)" )
57- statement.executeUpdate()
58- createIndex(" uniqueId" )
59- createIndex(" name" )
49+ getConnection().use { connection ->
50+ connection.prepareStatement(" CREATE TABLE IF NOT EXISTS `$playerCollectionName ` (`uniqueId` varchar(36), `name` varchar(16), `data` LONGBLOB)" )
51+ .use { statement ->
52+ statement.executeUpdate()
53+ createIndex(" uniqueId" )
54+ createIndex(" name" )
55+ }
56+ }
6057 }
6158 }
6259
63- private fun runReconnectLoop () {
64- reconnect()
65- Launcher .instance.scheduler.scheduleAtFixedRate({
66- reconnect()
67- }, 1 , 1 , TimeUnit .HOURS )
68- }
69-
70- private fun reconnect () = synchronized(this ) {
71- closeConnection()
72- this .connection =
73- DriverManager .getConnection(" jdbc:mysql://${databaseConnectionInformation.host} :${databaseConnectionInformation.port} /${databaseConnectionInformation.databaseName} ?user=${databaseConnectionInformation.userName} &password=${databaseConnectionInformation.password} &serverTimezone=UTC&autoReconnect=true" )
60+ private fun getConnection (): Connection {
61+ return DriverManager .getConnection(" jdbc:mysql://${databaseConnectionInformation.host} :${databaseConnectionInformation.port} /${databaseConnectionInformation.databaseName} ?user=${databaseConnectionInformation.userName} &password=${databaseConnectionInformation.password} &serverTimezone=UTC&autoReconnect=true" )
7462 }
7563
7664 private fun createIndex (columnName : String ) {
77- val statement = connection!! .prepareStatement(" ALTER TABLE $playerCollectionName ADD INDEX ($columnName )" )
78- statement.executeUpdate()
65+ getConnection().use { connection ->
66+ connection.prepareStatement(" ALTER TABLE $playerCollectionName ADD INDEX ($columnName )" )
67+ .use { statement ->
68+ statement.executeUpdate()
69+ }
70+ }
7971 }
8072
8173 override fun getOfflinePlayer (playerUniqueId : UUID ): IOfflineCloudPlayer ? {
@@ -88,13 +80,17 @@ class SQLOfflineCloudPlayerHandler(private val databaseConnectionInformation: Da
8880
8981 private fun loadPlayer (value : String , fieldName : String ): IOfflineCloudPlayer ? = synchronized(this ) {
9082 if (! exist(value, fieldName)) return null
91- val statement =
92- connection!! .prepareStatement(" SELECT `data` FROM `$playerCollectionName ` WHERE `$fieldName ` = ?" )
93- statement.setString(1 , value)
94- val resultSet = statement.executeQuery()
95- val allDataStrings = getAllDataStringsFromResultSet(resultSet)
96- val players = allDataStrings.mapNotNull { loadPlayerFromJsonString(it) }
97- return getPlayerWithLatestLogin(players)
83+
84+ getConnection().use { conn ->
85+ conn.prepareStatement(" SELECT `data` FROM `$playerCollectionName ` WHERE `$fieldName ` = ?" )
86+ .use { statement ->
87+ statement.setString(1 , value)
88+ val resultSet = statement.executeQuery()
89+ val allDataStrings = getAllDataStringsFromResultSet(resultSet)
90+ val players = allDataStrings.mapNotNull { loadPlayerFromJsonString(it) }
91+ return getPlayerWithLatestLogin(players)
92+ }
93+ }
9894 }
9995
10096 private fun getAllDataStringsFromResultSet (resultSet : ResultSet ): List <String > {
@@ -117,50 +113,62 @@ class SQLOfflineCloudPlayerHandler(private val databaseConnectionInformation: Da
117113
118114 override fun saveCloudPlayer (offlineCloudPlayer : OfflineCloudPlayer ): Unit = synchronized(this ) {
119115 val newData = JsonLib .fromObject(offlineCloudPlayer, databaseGson).getAsJsonString()
120- if (! exist(offlineCloudPlayer.getUniqueId().toString(), " uniqueId" )) {
121- val statement =
122- connection!! .prepareStatement(" INSERT INTO `$playerCollectionName ` (`uniqueId`, `name`, `data`) VALUES (?, ?, ?)" )
123- statement.setString(1 , offlineCloudPlayer.getUniqueId().toString())
124- statement.setString(2 , offlineCloudPlayer.getName())
125- statement.setString(3 , newData)
126- statement.executeUpdate()
127- } else {
128- val statement =
129- connection!! .prepareStatement(" UPDATE `$playerCollectionName ` SET `data` = ?, `name` = ? WHERE `uniqueId` = ?" )
130- statement.setString(1 , newData)
131- statement.setString(2 , offlineCloudPlayer.getName())
132- statement.setString(3 , offlineCloudPlayer.getUniqueId().toString())
133- statement.executeUpdate()
116+
117+ getConnection().use { connection ->
118+ if (! exist(offlineCloudPlayer.getUniqueId().toString(), " uniqueId" )) {
119+ connection.prepareStatement(" INSERT INTO `$playerCollectionName ` (`uniqueId`, `name`, `data`) VALUES (?, ?, ?)" )
120+ .use { statement ->
121+ statement.setString(1 , offlineCloudPlayer.getUniqueId().toString())
122+ statement.setString(2 , offlineCloudPlayer.getName())
123+ statement.setString(3 , newData)
124+ statement.executeUpdate()
125+ }
126+ } else {
127+ connection.prepareStatement(" UPDATE `$playerCollectionName ` SET `data` = ?, `name` = ? WHERE `uniqueId` = ?" )
128+ .use { statement ->
129+ statement.setString(1 , newData)
130+ statement.setString(2 , offlineCloudPlayer.getName())
131+ statement.setString(3 , offlineCloudPlayer.getUniqueId().toString())
132+ statement.executeUpdate()
133+ }
134+ }
134135 }
135136 }
136137
137138 override fun getRegisteredPlayerCount (): Int {
138- val statement = connection!! .prepareStatement(" SELECT COUNT(*) FROM `$playerCollectionName `" )
139- val resultSet = statement.executeQuery()
140- return if (! resultSet.next()) {
141- 0
142- } else {
143- resultSet.getInt(1 )
139+ getConnection().use { connection ->
140+ connection.prepareStatement(" SELECT COUNT(*) FROM `$playerCollectionName `" ).use { statement ->
141+ val resultSet = statement.executeQuery()
142+ return if (! resultSet.next()) {
143+ 0
144+ } else {
145+ resultSet.getInt(1 )
146+ }
147+ }
144148 }
145149 }
146150
147151 override fun closeConnection () {
148- connection?.close()
152+ // nothing to do
149153 }
150154
151155 private fun exist (searchValue : String , fieldName : String ): Boolean {
152- val prepareStatement =
153- connection!! .prepareStatement(" SELECT `data` FROM `$playerCollectionName ` WHERE `$fieldName ` = ?" )
154- prepareStatement.setString(1 , searchValue)
155- val resultSet = prepareStatement.executeQuery()
156- return resultSet.next()
156+ getConnection().use { connection ->
157+ connection.prepareStatement(" SELECT `data` FROM `$playerCollectionName ` WHERE `$fieldName ` = ?" )
158+ .use { statement ->
159+ statement.setString(1 , searchValue)
160+ val resultSet = statement.executeQuery()
161+ return resultSet.next()
162+ }
163+ }
157164 }
158165
159166 private fun doesTableExist (): Boolean {
160- val meta: DatabaseMetaData = connection!! .metaData
161- val res = meta.getTables(null , null , this .playerCollectionName, arrayOf(" TABLE" ))
162- return res.next()
167+ getConnection().use { connection ->
168+ val meta: DatabaseMetaData = connection.metaData
169+ val res = meta.getTables(null , null , this .playerCollectionName, arrayOf(" TABLE" ))
170+ return res.next()
171+ }
163172 }
164173
165-
166174}
0 commit comments