11package me .hsgamer .topper .storage .simple .supplier ;
22
33import me .hsgamer .hscore .database .client .sql .BatchBuilder ;
4+ import me .hsgamer .hscore .database .client .sql .SqlClient ;
45import me .hsgamer .hscore .database .client .sql .StatementBuilder ;
56import me .hsgamer .hscore .logger .common .LogLevel ;
67import me .hsgamer .hscore .logger .common .Logger ;
@@ -19,23 +20,48 @@ public abstract class SqlStorageSupplier implements DataStorageSupplier {
1920 protected final Logger logger = LoggerProvider .getLogger (getClass ());
2021 private final Lock lock = new ReentrantLock ();
2122
22- protected abstract Connection getConnection () throws SQLException ;
23+ protected abstract SqlClient <?> getClient () ;
2324
24- protected abstract void flushConnection (Connection connection );
25-
26- protected boolean shouldLockWhenModify () {
25+ protected boolean isSingleThread () {
2726 return false ;
2827 }
2928
3029 protected abstract List <String > toSaveStatement (String name , String [] keyColumns , String [] valueColumns );
3130
3231 protected abstract List <Object []> toSaveValues (Object [] keys , Object [] values );
3332
33+ private Connection getConnection () throws SQLException {
34+ Connection connection = getClient ().getConnection ();
35+ connection .setAutoCommit (false );
36+ return connection ;
37+ }
38+
39+ private void flushConnection (Connection connection ) {
40+ try {
41+ connection .close ();
42+ } catch (SQLException e ) {
43+ logger .log (LogLevel .ERROR , "Failed to close connection" , e );
44+ }
45+ }
46+
47+ private void lock () {
48+ if (isSingleThread ()) {
49+ lock .lock ();
50+ }
51+ }
52+
53+ private void unlock () {
54+ if (isSingleThread ()) {
55+ lock .unlock ();
56+ }
57+ }
58+
3459 @ Override
3560 public <K , V > DataStorage <K , V > getStorage (String name , ValueConverter <K > keyConverter , ValueConverter <V > valueConverter ) {
3661 return new DataStorage <K , V >() {
3762 @ Override
3863 public Map <K , V > load () {
64+ lock ();
3965 Connection connection = null ;
4066 try {
4167 connection = getConnection ();
@@ -52,11 +78,13 @@ public Map<K, V> load() {
5278 if (connection != null ) {
5379 flushConnection (connection );
5480 }
81+ unlock ();
5582 }
5683 }
5784
5885 @ Override
5986 public Optional <V > load (K key ) {
87+ lock ();
6088 Connection connection = null ;
6189 try {
6290 connection = getConnection ();
@@ -88,14 +116,13 @@ public Optional<V> load(K key) {
88116 if (connection != null ) {
89117 flushConnection (connection );
90118 }
119+ unlock ();
91120 }
92121 }
93122
94123 @ Override
95124 public Optional <Modifier <K , V >> modify () {
96- if (shouldLockWhenModify ()) {
97- lock .lock ();
98- }
125+ lock ();
99126 try {
100127 Connection connection = getConnection ();
101128 Modifier <K , V > modifier = new Modifier <K , V >() {
@@ -154,9 +181,7 @@ public void commit() {
154181 logger .log (LogLevel .ERROR , "Failed to commit" , e );
155182 } finally {
156183 flushConnection (connection );
157- if (shouldLockWhenModify ()) {
158- lock .unlock ();
159- }
184+ unlock ();
160185 }
161186 }
162187
@@ -168,21 +193,21 @@ public void rollback() {
168193 logger .log (LogLevel .ERROR , "Failed to rollback" , e );
169194 } finally {
170195 flushConnection (connection );
171- if (shouldLockWhenModify ()) {
172- lock .unlock ();
173- }
196+ unlock ();
174197 }
175198 }
176199 };
177200 return Optional .of (modifier );
178201 } catch (SQLException e ) {
179202 logger .log (LogLevel .ERROR , "Failed to get connection" , e );
203+ unlock ();
180204 return Optional .empty ();
181205 }
182206 }
183207
184208 @ Override
185209 public void onRegister () {
210+ lock ();
186211 Connection connection = null ;
187212 try {
188213 connection = getConnection ();
@@ -224,6 +249,7 @@ public void onRegister() {
224249 if (connection != null ) {
225250 flushConnection (connection );
226251 }
252+ unlock ();
227253 }
228254 }
229255 };
0 commit comments