通过检查Map大小来连接到不同数据库的不同

时间:2013-02-16 05:56:42

标签: java database multithreading

我有一个看起来像这样的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映射传递给这样的各种线程,并且不会被任何线程修改(通过进行set调用)。每个线程都将使用get方法来获取所需的方法。

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

所以在run方法中我需要在tableList大小上做出不同的conenctions基础。因此,如果tableList大小为2,那意味着我需要为两个不同的数据库创建两个不同的连接,callableStatements和方法。

问题: -

与我在run method中为tableList size创建与数据库基础的不同连接的方式相比,还有更好的方法吗?

下面是我的Task类,它强制运行Runnable Interface

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的方法。

1 个答案:

答案 0 :(得分:0)

好的,我仍然不确定该任务是如何使用它获得的数据。但是,我会将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()
    }
}

}