连接到许多 SQL 节点的 Java 应用程序

发布于 2024-12-12 01:06:02 字数 2468 浏览 0 评论 0原文

我有一个在 tomcat 上运行的 java 应用程序。 我想将它连接到我的 MySQL 集群。

在集群中我有三个 SQL 节点。 我想尝试连接到所有三个节点,然后采用最快返回给我的连接!

我该怎么做呢?表现对我来说真的很重要。

到目前为止,这是我所拥有的:

连接器类

    public class Connecter extends Thread {

    String dbURL;
    String dbDriver = "com.mysql.jdbc.Driver";
    Connection dbCon = null;

    public Connecter(String dbURL) {
        this.dbURL = dbURL;
    }

    @Override
    public void run() {
        try {
            Class.forName(dbDriver);
            try {
                dbCon = DriverManager.getConnection(dbURL, "root", "");
            } catch (SQLException ex) {
            }
        } catch (ClassNotFoundException ex) {
        }
    }

}

更近的类

public void run() {
        try {
            dbCon.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (NullPointerException e) {}
    }

以及尝试通过此方法连接的 DbBean:

String dbURL1 = "jdbc:mysql://192.168.0.3/bank";
String dbURL2 = "jdbc:mysql://192.168.0.4/bank";
String dbURL3 = "jdbc:mysql://192.168.0.5/bank";
String dbDriver = "com.mysql.jdbc.Driver";
private Connection dbCon;

public boolean connect() throws ClassNotFoundException, SQLException, InterruptedException {
    Class.forName(dbDriver);

    Connecter one = new Connecter(dbURL1);
    Connecter two = new Connecter(dbURL2);
    Connecter three = new Connecter(dbURL3);

    Closer a = new Closer (one.dbCon);
    Closer b = new Closer (two.dbCon);
    Closer c = new Closer (three.dbCon);

    one.start();
    two.start();
    three.start();

    while(one.isAlive() && two.isAlive() && three.isAlive()){
        Thread.sleep(10);
    }

    if(one.dbCon != null) {
        this.dbCon = one.dbCon;
        two.interrupt();
        b.start();
        three.interrupt();
        c.start();
        return true;
    } else {
        one.interrupt();
        a.start();
    }
    if(two.dbCon != null) {
        this.dbCon = two.dbCon;
        one.interrupt();
        a.start();
        three.interrupt();
        c.start();
        return true;
    } else {
        two.interrupt();
        b.start();
    }
    if(three.dbCon != null) {
        this.dbCon = three.dbCon;
        one.interrupt();
        a.start();
        two.interrupt();
        b.start();
        return true;
    } else {
        three.interrupt();
        c.start();
    }

    return false;
}

I have a java application running on tomcat.
I want to connect it to my MySQL cluster.

In the cluster I have three SQL nodes.
I want to attempt a connection to all three nodes and then take the connection that returns to me the fastest!

How can i go about doing this? Performance is really important to me.

Here is what i have thus far:

A connector class

    public class Connecter extends Thread {

    String dbURL;
    String dbDriver = "com.mysql.jdbc.Driver";
    Connection dbCon = null;

    public Connecter(String dbURL) {
        this.dbURL = dbURL;
    }

    @Override
    public void run() {
        try {
            Class.forName(dbDriver);
            try {
                dbCon = DriverManager.getConnection(dbURL, "root", "");
            } catch (SQLException ex) {
            }
        } catch (ClassNotFoundException ex) {
        }
    }

}

A closer class

public void run() {
        try {
            dbCon.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (NullPointerException e) {}
    }

And a DbBean attempting to connect via this method:

String dbURL1 = "jdbc:mysql://192.168.0.3/bank";
String dbURL2 = "jdbc:mysql://192.168.0.4/bank";
String dbURL3 = "jdbc:mysql://192.168.0.5/bank";
String dbDriver = "com.mysql.jdbc.Driver";
private Connection dbCon;

public boolean connect() throws ClassNotFoundException, SQLException, InterruptedException {
    Class.forName(dbDriver);

    Connecter one = new Connecter(dbURL1);
    Connecter two = new Connecter(dbURL2);
    Connecter three = new Connecter(dbURL3);

    Closer a = new Closer (one.dbCon);
    Closer b = new Closer (two.dbCon);
    Closer c = new Closer (three.dbCon);

    one.start();
    two.start();
    three.start();

    while(one.isAlive() && two.isAlive() && three.isAlive()){
        Thread.sleep(10);
    }

    if(one.dbCon != null) {
        this.dbCon = one.dbCon;
        two.interrupt();
        b.start();
        three.interrupt();
        c.start();
        return true;
    } else {
        one.interrupt();
        a.start();
    }
    if(two.dbCon != null) {
        this.dbCon = two.dbCon;
        one.interrupt();
        a.start();
        three.interrupt();
        c.start();
        return true;
    } else {
        two.interrupt();
        b.start();
    }
    if(three.dbCon != null) {
        this.dbCon = three.dbCon;
        one.interrupt();
        a.start();
        two.interrupt();
        b.start();
        return true;
    } else {
        three.interrupt();
        c.start();
    }

    return false;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初懵 2024-12-19 01:06:02

只需使用支持连接池的数据库(任何现代 RDBMS 都支持这一点)并使用开箱即用的解决方案,例如 DataSource 或 c3p0。

例如,请参阅官方 JDBC 教程< /a>.

我不确定的一件事是您是否需要连接池或更高级的集群。但是,无论如何,我怀疑 MySQL 能否提供生产稳定的集群,而且据我所知,Oracle 通过其 JDBC 驱动程序透明地支持集群。

Just use database which supports connection pooling (any modern RDBMS supports this) and use out-of-the-box solution, such as DataSource or c3p0.

See, for example, official JDBC tutorial.

One thing I'm not sure about, is whether you want pool of connections or more advanced clustering. But, anyway, I doubt MySQL can provide production-stable clustering and, as far as I know, Oracle supports clustering transparently, by means of it's JDBC driver.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文