Java JDBC查询在单独的线程锁父级中

前端之家收集整理的这篇文章主要介绍了Java JDBC查询在单独的线程锁父级中前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在理解这个问题上遇到了一些麻烦.
这是正在发生的事情.
我正在生成一个新的线程,它保存与Oracle数据库的JDBC连接.
当我要求它连接到数据库时,父线程在调用start()方法时继续运行但是当我要求子进行查询时(在单独的方法上),父线程被卡住等待子线程的完成工作的方法.
有什么猜测如何解决这个问题?
提前致谢!
public class Main extends Thread{

    public Main()
    {
    }

    public void myCounter() {
        int i = 0;
        DBConnection myConnection = null;
        for(;;)
        {
            i++;

            System.out.println("time: " + i);
            if( i  == 5)
            {
                myConnection = new DBConnection("localhost",1521,"hr","XE");
                myConnection.start();


            }
            if(i == 10)
                try {

                    myConnection.runQuery("Select * from hr.numbers order by dbms_random.value");
                } catch (sqlException e) {
                    e.printStackTrace();
                }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void run()
    {
        myCounter();
    }

    public static void main(String[] args) {

        Main boot = new Main();
        boot.start();

    }

}
public class DBConnection extends Thread{

    Connection myConnection;
    int port;
    String user;
    String password;
    String serviceName;
    String host;


    public void run()
    {
        setUpConnection(host,port,user,password,serviceName);
    }
    /**
     * Sets up variables to create a connection to Oracle.
     *  
     * @param host      host
     * @param port      port
     * @param user      user
     * @param password  password
     */
    public DBConnection(String host,int port,String user,String password,String serviceName)
    {
        this.host = host;
        this.port = port;
        this.user = user;
        this.password = password;
        this.serviceName = serviceName;
    }


    private void setUpConnection(String host,String dataBase) {
        System.out.println("-------- Oracle "
                + "JDBC Connection Testing ------------");

        try {

            Class.forName("oracle.jdbc.OracleDriver");

        } catch (ClassNotFoundException e) {

            System.out.println("Couldn't find Oracle JDBC Driver... :-(");
            e.printStackTrace();
            return;

        }

        System.out.println("Oracle JDBC Driver Registered!");
        myConnection = null;
        try {
            myConnection = DriverManager.getConnection(
                    "jdbc:oracle:thin:@//" 
                            + host 
                            + ":" 
                            + port 
                            + "/" 
                            + dataBase,password
                    );

        } catch (sqlException e) {

            System.out.println("Connection Failed!");
            e.printStackTrace();
            return;

        }

        if (myConnection != null) {
            System.out.println("Connected to Oracle! :-)");
        } else {
            System.out.println("Failed to make connection!");
        }
    }


    /**
     * Queries the database and returns a ResultSet
     * @param  query            sql
     * @throws sqlException 
     */
    public  ResultSet runQuery(String query) throws sqlException 
    {
        System.out.println("                                                    [DBConnection] Started Running @ " + (new SimpleDateFormat("HH:mm:ss:S")).format(new Date()));
        ResultSet rs = null;

        Statement stt = myConnection.createStatement();
        rs = stt.executeQuery(query);
        System.out.println("                                                    [DBConnection] Finished Running @: " + (new SimpleDateFormat("HH:mm:ss:S")).format(new Date()));
        return  rs;

    }

这是我得到的输出

time: 1
time: 2
time: 3
time: 4
time: 5
-------- Oracle JDBC Connection Testing ------------
Oracle JDBC Driver Registered!
time: 6
Connected to Oracle! :-)
time: 7
time: 8
time: 9
time: 10
                                                    [DBConnection] Started Running @ 14:46:00:660
                                                    [DBConnection] Finished Running @: 14:46:12:750
time: 11
time: 12
time: 13
time: 14



..
.

解决方法

我认为你误解了线程的工作原理.问题是你在父线程上调用它:
myConnection.runQuery("Select * from hr.numbers order by dbms_random.value");

这是对myConnection对象上的runQuery方法的顺序调用,恰好是一个线程.这并不意味着它会指示孩子执行该方法.相反,父级将自己执行它,并且子线程在其run方法返回后立即完成.

如果您希望有一个单独的线程继续接收执行查询的命令,则必须实现生产者 – 消费者模式,其中父级保持排队命令以便子进程执行.为此我建议你看看ExecutorService.

猜你在找的Java相关文章