gpt4 book ai didi

java - 在多线程代码中的两个表中存储相同的 ID

转载 作者:行者123 更新时间:2023-12-01 04:52:00 25 4
gpt4 key购买 nike

我已经开始从事一个项目,其中我在不同的数据库中有两个具有不同架构的表。因此,我将使用两个不同的连接参数来连接到数据库。连接到每个数据库后,我需要使用相应表中给出的sql插入这两个表。

我应该根据命令行参数使用 JDBC 插入所有两个表或其中任何一个表。因此,这意味着单个线程将插入 Table1 和 Table2 或其中任何一个。

命令行参数:- 这里 10 是线程数,100 是任务数,table1 和 table2 是表名称。

10 100 table1 table2

下面是我的代码。在此代码中,将发生的情况是 - 假设如果我们仅传递一个名为 table1 的表,那么它将使用适用于 table1 的 SQL 插入到该表中。

而且我还需要在 table1table2 中插入相同的 id 并且正在传递 id作为构造函数TaskAtomicInteger。因此,这意味着如果 id 为 1,则此 1 id 应该同时存在于 table1table2 中。

 final AtomicInteger id = new AtomicInteger(1);

ExecutorService service = Executors. newFixedThreadPool(noOfThreads);

for (int i = 0; i < noOfTasks * noOfThreads; i++) {

for (String arg : tableNames) {
String url = (String) prop.get(arg + ".url");
String user = (String) prop.get(arg + ".user");
String password = (String) prop.get(arg + ".password");
String driver = (String) prop.get(arg + ".driver");
String suffix = (String) prop.get(arg + ".suffix");
String sql = (String) prop.get(arg + ".sql");

service.submit( new Task(id, url, user, password, driver, sql, suffix));
}
}

下面是实现Runnable接口(interface)的Task类

class Task implements Runnable {

private final AtomicInteger id ;
private final String url ;
private final String username ;
private final String password ;
private final String sql ;
private final String driver ;
private final String suffix ;

public Task(AtomicInteger id, String url, String user, String password, String driver, String sql, String suffix) {
this.id = id;
this.url = url;
this.username = user;
this.password = password;
this.driver = driver;
this.sql = sql;
this.suffix = suffix;
}

@Override
public void run() {

try {

dbConnection = getDBConnection(url , username , password , driver );
callableStatement = dbConnection .prepareCall(sql);

int userId = id .getAndIncrement();

callableStatement.setString(1, String.valueOf(userId));

//other callableStatement


callableStatement.executeUpdate();
}
}

因此,如果我使用多个线程运行上述程序,例如线程数为 10,任务数为 1000id if我选择1

然后在两个表中不存在相同的 id,这意味着 id 1 将仅在一个表中存在,即 table1table2。我能想到的唯一原因是 - idAtomicInteger 所以每次它都会为每个线程获得一个新的 id 。有什么方法可以使用相同的 id 插入每个表中吗?然后确保 id 是PrimaryKey,这样每个线程在再次插入这些表时都会获得一个新的 id。

最佳答案

如果我理解正确,您应该将计数器定义为静态以避免重复的PrimaryKey:

static AtomicInteger id;

然后将增量代码移至主循环,如下所示(不要忘记删除 Task 类中的 int userId = id .getAndIncrement(); ):

for (int i = 0; i < noOfTasks * noOfThreads; i++) {
// add following line
int userId = id.getAndIncrement();

for (String arg : tableNames) {
String url = (String) prop.get(arg + ".url");
String user = (String) prop.get(arg + ".user");
String password = (String) prop.get(arg + ".password");
String driver = (String) prop.get(arg + ".driver");
String suffix = (String) prop.get(arg + ".suffix");
String sql = (String) prop.get(arg + ".sql");

service.submit( new Task(id, url, user, password, driver, sql, suffix));
}
}

此外,由于 id 计数器保存在内存中,您可能需要将其持久化,否则在重新启动应用程序后它将重置为 0。

关于java - 在多线程代码中的两个表中存储相同的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14803357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com