Skip to content

Commit 5536a5f

Browse files
committed
fix getConnection before acquire mainConnLock
1 parent 38956c9 commit 5536a5f

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

src/main/java/cat/nyaa/nyaacore/database/provider/SQLiteDatabase.java

+17-11
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,13 @@ protected Object clone() throws CloneNotSupportedException {
9696
@Override
9797
@SuppressWarnings("rawtypes")
9898
public void createTable(Class<?> cls) {
99-
mainConnLock.acquireUninterruptibly();
99+
try {
100+
if (!mainConnLock.tryAcquire(10, TimeUnit.SECONDS)) {
101+
throw new IllegalStateException();
102+
}
103+
} catch (InterruptedException e) {
104+
throw new RuntimeException(e);
105+
}
100106
try {
101107
Validate.notNull(cls);
102108
if (createdTableClasses.contains(cls)) return;
@@ -150,27 +156,27 @@ public void rollbackTransaction() {
150156
@Override
151157
public <T> SynchronizedQuery.TransactionalQuery<T> queryTransactional(Class<T> tableClass) {
152158
Connection conn;
153-
try {
154-
createTable(tableClass);
155-
conn = getConnection();
156-
conn.setAutoCommit(false);
157-
} catch (Throwable ex) {
158-
mainConnLock.release();
159-
throw new RuntimeException(ex);
160-
}
159+
createTable(tableClass);
161160
try {
162161
if (!mainConnLock.tryAcquire(10, TimeUnit.SECONDS)) {
163162
throw new IllegalStateException();
164163
}
165164
} catch (InterruptedException e) {
166165
throw new RuntimeException(e);
167166
}
167+
try {
168+
conn = getConnection();
169+
conn.setAutoCommit(false);
170+
} catch (Throwable ex) {
171+
mainConnLock.release();
172+
throw new RuntimeException(ex);
173+
}
168174
SQLiteQuery<T> sqliteQuery = new SQLiteQuery<>(tableClass, conn, logger);
169175
FinalizablePhantomReference<SynchronizedQuery.TransactionalQuery<T>> reference = new FinalizablePhantomReference<SynchronizedQuery.TransactionalQuery<T>>(sqliteQuery, frq) {
170176
public void finalizeReferent() {
171177
Semaphore lock = references.remove(this);
172178
if (lock != null) {
173-
logger.severe("Unhandled TransactionalQuery found");
179+
logger.severe("Unhandled TransactionalQuery found: " + this);
174180
try {
175181
if (conn.isValid(1) && !conn.getAutoCommit()) {
176182
conn.rollback();
@@ -180,7 +186,7 @@ public void finalizeReferent() {
180186
logger.log(Level.SEVERE, "Bad connection!", e);
181187
}
182188
dbConn = null;
183-
dbConn = newConnection();
189+
dbConn = createConnection();
184190
lock.release();
185191
}
186192
}

0 commit comments

Comments
 (0)