Skip to content

Commit

Permalink
fix(async): 尝试修复 #49 中提到的问题
Browse files Browse the repository at this point in the history
fix(async): 尝试修复 #49 中提到的问题
  • Loading branch information
CarmJos authored Jun 22, 2022
2 parents 421fe9f + 0495928 commit 248a6d6
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 19 deletions.
2 changes: 1 addition & 1 deletion api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
19 changes: 19 additions & 0 deletions api/src/main/java/cc/carm/lib/easysql/api/SQLManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
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;

/**
Expand All @@ -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()} 来输出调试信息。
Expand Down
8 changes: 2 additions & 6 deletions demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down Expand Up @@ -70,36 +70,31 @@
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>4.0.3</version>
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
<dependency>
<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>
Expand All @@ -120,4 +115,5 @@
</plugin>
</plugins>
</build>

</project>
5 changes: 3 additions & 2 deletions demo/src/test/java/cc/carm/lib/easysql/EasySQLTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;");
Expand All @@ -28,7 +28,7 @@ public void initDatabase() {
}

@After
public void shutdownDatabase() {
public void shutdown() {
if (sqlManager.getDataSource() instanceof HikariDataSource) {
//Close bee connection pool
((HikariDataSource) sqlManager.getDataSource()).close();
Expand All @@ -50,6 +50,7 @@ public void onTest() {
tests.add(new SQLUpdateReturnKeysTest());
tests.add(new QueryCloseTest());
tests.add(new QueryFunctionTest());
tests.add(new QueryAsyncTest());
// tests.add(new DeleteTest());

print("准备进行测试...");
Expand Down
33 changes: 33 additions & 0 deletions demo/src/test/java/cc/carm/lib/easysql/tests/QueryAsyncTest.java
Original file line number Diff line number Diff line change
@@ -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"));
}
});

}
}
2 changes: 1 addition & 1 deletion impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ public QueryActionImpl(@NotNull SQLManagerImpl manager, @NotNull String sql) {

@Override
public void executeAsync(SQLHandler<SQLQuery> success, SQLExceptionHandler failure) {
try (SQLQueryImpl query = execute()) {
if (success != null) success.accept(query);
} catch (SQLException exception) {
handleException(failure, exception);
}
getManager().getExecutorPool().submit(() -> {
try (SQLQueryImpl query = execute()) {
if (success != null) success.accept(query);
} catch (SQLException exception) {
handleException(failure, exception);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,14 @@ public Logger getLogger() {
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;
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion with-pool/beecp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion with-pool/hikaricp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit 248a6d6

Please sign in to comment.