0

我有一个看起来像这样的 HashMap -

 HashMap<String, TableConnectionInfo> tableList

这意味着它的值是一个TableConnectionInfo看起来像这样的类 -

public class TableConnectionInfo {

    public String url;
    public String user;
    public String password;
    public String driver;
    public String suffix;
    public String sql;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getDriver() {
        return driver;
    }

    public void setDriver(String driver) {
        this.driver = driver;
    }

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }

    public String getSql() {
        return sql;
    }

    public void setSql(String sql) {
        this.sql = sql;
    }

}

所以假设,如果我在上面有两个值HashMap。这意味着,我需要对两个不同的数据库建立两个不同的连接。并假设如果该映射具有三个值,那么我需要对三个不同的数据库建立三个不同的连接。

在主线程中,我通过像这样从属性文件中读取上面的地图来填充上面的地图,之后这个地图将不会被修改。

for (String arg : databaseNames) {

    TableConnectionInfo ci = new TableConnectionInfo();

    String url = prop.getProperty(arg + ".url");
    String user = prop.getProperty(arg + ".user");
    String password = prop.getProperty(arg + ".password");
    String driver = prop.getProperty(arg + ".driver");
    String suffix = prop.getProperty(arg + ".suffix");
    String sql = prop.getProperty(arg + ".sql");

    ci.setUrl(url);
    ci.setDriver(driver);
    ci.setPassword(password);
    ci.setSql(sql);
    ci.setSuffix(suffix);
    ci.setUser(user);
    tableList.put(arg, ci);
}

现在我将此tableList映射传递给这样的各种线程,并且它不会被任何线程修改(通过进行集合调用)。每个线程都将使用 get 方法来获取所需的方法。

for (int i = 0; i< 1000; i++) {
    service.submit(new Task(tableList));
}

所以在运行方法中我需要根据tableList大小进行不同的连接。因此,如果tableList大小为 2,则意味着我需要对两个不同的数据库进行两个不同的连接、callableStatements 和方法。

问题:-

那么,与我run method在创建与数据库的不同连接的方式相比,有什么更好的方法tableList size吗?

下面是我的包含可运行接口的任务类

class Task implements Runnable {

    private Connection[] dbConnection = null;
    private CallableStatement[] callableStatement = null;
    private ArrayList<Method> methods[] = null;

    private final HashMap<String, TableConnectionInfo> tableLists;

    public Task(HashMap<String, TableConnectionInfo> tableList) {
        this.tableLists = tableList;
    }

    @Override
    public void run() {

        try {

            int j = 0;
            dbConnection = new Connection[tableLists.size()];
            callableStatement = new CallableStatement[tableLists.size()];
            methods = new ArrayList[tableLists.size()];

            for (TableConnectionInfo ci : tableLists.values()) {

                dbConnection[j] = getDBConnection(ci.getUrl(), ci.getUser(), ci.getPassword(),  ci.getDriver());
                callableStatement[j] = dbConnection[j].prepareCall(ci.getSql());

                methods[j] = getRequiredMethods(ci.getSuffix());
                j++;
            }

          }
             }
       }

与该数据库建立连接-

private Connection getDBConnection(String url, String username, String password, String driver) {

        Connection dbConnection = null;

        try {
            Class.forName(driver);
            dbConnection = DriverManager.getConnection(url, username, password);
        } 

        return dbConnection;
    }

只需在此处添加即可getRequiredMethods获取特定表的所有方法名称。因此,假设如果 tableList 大小为 1,那么我们将只有一个与该数据库的连接,因此getRequiredMethods将获取所有方法table1并将其存储在 ArrayList 中。但是假设如果 tableList 的大小为 2,那么我们将有两个不同的连接到两个不同的数据库,这就是我将方法作为数组创建的原因,以便它可以保存表 1 的方法和表 2 的方法。

4

1 回答 1

0

好的,我仍然不确定 Task 是如何使用它获取的数据的。但是,我会将 getConnection、getCallableStatement 和 getMethods() 函数移到 TableConnectionInfo 上的方法中。您可以简单地创建一个 TableConnectionInfo 集合(如您已经拥有的那样进行初始化,存储在一个 ArrayList 中)。然后 Runnable 简单地遍历 TableConnectionInfo。

public class TableConnectionInfo {

    private String url;
    private String user;
    private String password;
    private String driver;
    private String suffix;
    private String sql;

private Connection connection;

<snip... getters and setters for the properties>

public Connection getConnection() {
    // TODO  create and return a connection
    if (connection == null) {
        // create the connection
    }
    return connection;
}

public CallableStatement getCallableStatement() {
    // get the callable statement
    return null;
}

public Collection<Method> getMethods() {
    // Get the Methods
    return null;
}

}

public class TableTask implements Runnable {

private Collection<TableConnectionInfo> tables;

public TableTask(Collection<TableConnectionInfo> tables) {
    this.tables = tables;
}

@Override
public void run() {
    for (TableConnectionInfo table : tables) {
        // do something with table.getConnection(), or table.getCallableStatement()
        // and/or table.getMethods()
    }
}

}
于 2013-02-16T08:52:26.103 回答