1
mirror of https://github.com/CarmJos/EasySQL.git synced 2024-09-19 21:35:47 +00:00

fix(async): 尝试修复 #49 中提到的问题

This commit is contained in:
Carm Jos 2022-06-22 20:42:08 +08:00
parent 421fe9f454
commit 0495928e49
11 changed files with 74 additions and 19 deletions

View File

@ -5,7 +5,7 @@
<parent>
<groupId>cc.carm.lib</groupId>
<artifactId>easysql-parent</artifactId>
<version>0.4.0</version>
<version>0.4.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -17,6 +17,8 @@ import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.function.Supplier;
/**
@ -30,6 +32,23 @@ public interface SQLManager {
boolean isDebugMode();
/**
* 获取用于执行 {@link SQLAction#executeAsync()} 的线程池
* <br> 默认线程池为 {@link ThreadPoolExecutor} ,大小为 3
*
* @return {@link ExecutorService}
*/
@NotNull ExecutorService getExecutorPool();
/**
* 设定用于执行 {@link SQLAction#executeAsync()} 的线程池
*
* @param executorPool {@link ExecutorService}
*/
void setExecutorPool(@NotNull ExecutorService executorPool);
/**
* 设定是否启用调试模式
* 启用调试模式后会在每次执行SQL语句时调用 {@link #getDebugHandler()} 来输出调试信息

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easysql-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>0.4.0</version>
<version>0.4.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
@ -70,7 +70,6 @@
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>4.0.3</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
@ -78,28 +77,24 @@
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.1.214</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j.version}</version>
<scope>compile</scope>
</dependency>
</dependencies>
@ -120,4 +115,5 @@
</plugin>
</plugins>
</build>
</project>

View File

@ -18,7 +18,7 @@ public class EasySQLTest {
protected SQLManager sqlManager;
@Before
public void initDatabase() {
public void initialize() {
HikariConfig config = new HikariConfig();
config.setDriverClassName("org.h2.Driver");
config.setJdbcUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;MODE=MYSQL;");
@ -28,7 +28,7 @@ public class EasySQLTest {
}
@After
public void shutdownDatabase() {
public void shutdown() {
if (sqlManager.getDataSource() instanceof HikariDataSource) {
//Close bee connection pool
((HikariDataSource) sqlManager.getDataSource()).close();
@ -50,6 +50,7 @@ public class EasySQLTest {
tests.add(new SQLUpdateReturnKeysTest());
tests.add(new QueryCloseTest());
tests.add(new QueryFunctionTest());
tests.add(new QueryAsyncTest());
// tests.add(new DeleteTest());
print("准备进行测试...");

View File

@ -0,0 +1,33 @@
package cc.carm.lib.easysql.tests;
import cc.carm.lib.easysql.TestHandler;
import cc.carm.lib.easysql.api.SQLManager;
import java.sql.SQLException;
public class QueryAsyncTest extends TestHandler {
@Override
public void onTest(SQLManager sqlManager) throws SQLException {
sqlManager.createQuery()
.inTable("test_user_table")
.orderBy("id", false)
.setLimit(1)
.build().executeAsync(query -> {
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
if (!query.getResultSet().next()) {
System.out.println("id (ps): NULL");
} else {
System.out.println("id (ps): " + query.getResultSet().getInt("id"));
}
});
}
}

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easysql-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>0.4.0</version>
<version>0.4.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -53,10 +53,12 @@ public class QueryActionImpl extends AbstractSQLAction<SQLQuery> implements Quer
@Override
public void executeAsync(SQLHandler<SQLQuery> success, SQLExceptionHandler failure) {
getManager().getExecutorPool().submit(() -> {
try (SQLQueryImpl query = execute()) {
if (success != null) success.accept(query);
} catch (SQLException exception) {
handleException(failure, exception);
}
});
}
}

View File

@ -89,10 +89,14 @@ public class SQLManagerImpl implements SQLManager {
return LOGGER;
}
public ExecutorService getExecutorPool() {
public @NotNull ExecutorService getExecutorPool() {
return executorPool;
}
public void setExecutorPool(@NotNull ExecutorService executorPool) {
this.executorPool = executorPool;
}
@Override
public @NotNull DataSource getDataSource() {
return this.dataSource;

View File

@ -19,7 +19,7 @@
<groupId>cc.carm.lib</groupId>
<artifactId>easysql-parent</artifactId>
<packaging>pom</packaging>
<version>0.4.0</version>
<version>0.4.1</version>
<modules>
<module>api</module>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easysql-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>0.4.0</version>
<version>0.4.1</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easysql-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>0.4.0</version>
<version>0.4.1</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>