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:
parent
421fe9f454
commit
0495928e49
@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<artifactId>easysql-parent</artifactId>
|
<artifactId>easysql-parent</artifactId>
|
||||||
<version>0.4.0</version>
|
<version>0.4.1</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
@ -17,6 +17,8 @@ import java.sql.SQLException;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.ThreadPoolExecutor;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -30,6 +32,23 @@ public interface SQLManager {
|
|||||||
|
|
||||||
boolean isDebugMode();
|
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()} 来输出调试信息。
|
* 启用调试模式后,会在每次执行SQL语句时,调用 {@link #getDebugHandler()} 来输出调试信息。
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easysql-parent</artifactId>
|
<artifactId>easysql-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>0.4.0</version>
|
<version>0.4.1</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
@ -70,7 +70,6 @@
|
|||||||
<groupId>com.zaxxer</groupId>
|
<groupId>com.zaxxer</groupId>
|
||||||
<artifactId>HikariCP</artifactId>
|
<artifactId>HikariCP</artifactId>
|
||||||
<version>4.0.3</version>
|
<version>4.0.3</version>
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
|
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
|
||||||
@ -78,28 +77,24 @@
|
|||||||
<groupId>com.h2database</groupId>
|
<groupId>com.h2database</groupId>
|
||||||
<artifactId>h2</artifactId>
|
<artifactId>h2</artifactId>
|
||||||
<version>2.1.214</version>
|
<version>2.1.214</version>
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.logging.log4j</groupId>
|
<groupId>org.apache.logging.log4j</groupId>
|
||||||
<artifactId>log4j-api</artifactId>
|
<artifactId>log4j-api</artifactId>
|
||||||
<version>${log4j.version}</version>
|
<version>${log4j.version}</version>
|
||||||
<scope>compile</scope>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.logging.log4j</groupId>
|
<groupId>org.apache.logging.log4j</groupId>
|
||||||
<artifactId>log4j-core</artifactId>
|
<artifactId>log4j-core</artifactId>
|
||||||
<version>${log4j.version}</version>
|
<version>${log4j.version}</version>
|
||||||
<scope>compile</scope>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.logging.log4j</groupId>
|
<groupId>org.apache.logging.log4j</groupId>
|
||||||
<artifactId>log4j-slf4j-impl</artifactId>
|
<artifactId>log4j-slf4j-impl</artifactId>
|
||||||
<version>${log4j.version}</version>
|
<version>${log4j.version}</version>
|
||||||
<scope>compile</scope>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
@ -120,4 +115,5 @@
|
|||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -18,7 +18,7 @@ public class EasySQLTest {
|
|||||||
protected SQLManager sqlManager;
|
protected SQLManager sqlManager;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void initDatabase() {
|
public void initialize() {
|
||||||
HikariConfig config = new HikariConfig();
|
HikariConfig config = new HikariConfig();
|
||||||
config.setDriverClassName("org.h2.Driver");
|
config.setDriverClassName("org.h2.Driver");
|
||||||
config.setJdbcUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;MODE=MYSQL;");
|
config.setJdbcUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;MODE=MYSQL;");
|
||||||
@ -28,7 +28,7 @@ public class EasySQLTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void shutdownDatabase() {
|
public void shutdown() {
|
||||||
if (sqlManager.getDataSource() instanceof HikariDataSource) {
|
if (sqlManager.getDataSource() instanceof HikariDataSource) {
|
||||||
//Close bee connection pool
|
//Close bee connection pool
|
||||||
((HikariDataSource) sqlManager.getDataSource()).close();
|
((HikariDataSource) sqlManager.getDataSource()).close();
|
||||||
@ -50,6 +50,7 @@ public class EasySQLTest {
|
|||||||
tests.add(new SQLUpdateReturnKeysTest());
|
tests.add(new SQLUpdateReturnKeysTest());
|
||||||
tests.add(new QueryCloseTest());
|
tests.add(new QueryCloseTest());
|
||||||
tests.add(new QueryFunctionTest());
|
tests.add(new QueryFunctionTest());
|
||||||
|
tests.add(new QueryAsyncTest());
|
||||||
// tests.add(new DeleteTest());
|
// tests.add(new DeleteTest());
|
||||||
|
|
||||||
print("准备进行测试...");
|
print("准备进行测试...");
|
||||||
|
@ -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"));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easysql-parent</artifactId>
|
<artifactId>easysql-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>0.4.0</version>
|
<version>0.4.1</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
@ -53,10 +53,12 @@ public class QueryActionImpl extends AbstractSQLAction<SQLQuery> implements Quer
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void executeAsync(SQLHandler<SQLQuery> success, SQLExceptionHandler failure) {
|
public void executeAsync(SQLHandler<SQLQuery> success, SQLExceptionHandler failure) {
|
||||||
try (SQLQueryImpl query = execute()) {
|
getManager().getExecutorPool().submit(() -> {
|
||||||
if (success != null) success.accept(query);
|
try (SQLQueryImpl query = execute()) {
|
||||||
} catch (SQLException exception) {
|
if (success != null) success.accept(query);
|
||||||
handleException(failure, exception);
|
} catch (SQLException exception) {
|
||||||
}
|
handleException(failure, exception);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -89,10 +89,14 @@ public class SQLManagerImpl implements SQLManager {
|
|||||||
return LOGGER;
|
return LOGGER;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ExecutorService getExecutorPool() {
|
public @NotNull ExecutorService getExecutorPool() {
|
||||||
return executorPool;
|
return executorPool;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setExecutorPool(@NotNull ExecutorService executorPool) {
|
||||||
|
this.executorPool = executorPool;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull DataSource getDataSource() {
|
public @NotNull DataSource getDataSource() {
|
||||||
return this.dataSource;
|
return this.dataSource;
|
||||||
|
2
pom.xml
2
pom.xml
@ -19,7 +19,7 @@
|
|||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<artifactId>easysql-parent</artifactId>
|
<artifactId>easysql-parent</artifactId>
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
<version>0.4.0</version>
|
<version>0.4.1</version>
|
||||||
|
|
||||||
<modules>
|
<modules>
|
||||||
<module>api</module>
|
<module>api</module>
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easysql-parent</artifactId>
|
<artifactId>easysql-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>0.4.0</version>
|
<version>0.4.1</version>
|
||||||
<relativePath>../../pom.xml</relativePath>
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easysql-parent</artifactId>
|
<artifactId>easysql-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>0.4.0</version>
|
<version>0.4.1</version>
|
||||||
<relativePath>../../pom.xml</relativePath>
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
Loading…
Reference in New Issue
Block a user