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

前端之家收集整理的这篇文章主要介绍了Java JDBC查询在单独的线程锁父级中前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在理解这个问题上遇到了一些麻烦.
这是正在发生的事情.
我正在生成一个新的线程,它保存与Oracle数据库的JDBC连接.
当我要求它连接到数据库时,父线程在调用start()方法时继续运行但是当我要求子进行查询时(在单独的方法上),父线程被卡住等待子线程的完成工作的方法.
有什么猜测如何解决这个问题?
提前致谢!
  1. public class Main extends Thread{
  2.  
  3. public Main()
  4. {
  5. }
  6.  
  7. public void myCounter() {
  8. int i = 0;
  9. DBConnection myConnection = null;
  10. for(;;)
  11. {
  12. i++;
  13.  
  14. System.out.println("time: " + i);
  15. if( i == 5)
  16. {
  17. myConnection = new DBConnection("localhost",1521,"hr","XE");
  18. myConnection.start();
  19.  
  20.  
  21. }
  22. if(i == 10)
  23. try {
  24.  
  25. myConnection.runQuery("Select * from hr.numbers order by dbms_random.value");
  26. } catch (sqlException e) {
  27. e.printStackTrace();
  28. }
  29. try {
  30. Thread.sleep(1000);
  31. } catch (InterruptedException e) {
  32. e.printStackTrace();
  33. }
  34. }
  35. }
  36.  
  37. public void run()
  38. {
  39. myCounter();
  40. }
  41.  
  42. public static void main(String[] args) {
  43.  
  44. Main boot = new Main();
  45. boot.start();
  46.  
  47. }
  48.  
  49. }
  1. public class DBConnection extends Thread{
  2.  
  3. Connection myConnection;
  4. int port;
  5. String user;
  6. String password;
  7. String serviceName;
  8. String host;
  9.  
  10.  
  11. public void run()
  12. {
  13. setUpConnection(host,port,user,password,serviceName);
  14. }
  15. /**
  16. * Sets up variables to create a connection to Oracle.
  17. *
  18. * @param host host
  19. * @param port port
  20. * @param user user
  21. * @param password password
  22. */
  23. public DBConnection(String host,int port,String user,String password,String serviceName)
  24. {
  25. this.host = host;
  26. this.port = port;
  27. this.user = user;
  28. this.password = password;
  29. this.serviceName = serviceName;
  30. }
  31.  
  32.  
  33. private void setUpConnection(String host,String dataBase) {
  34. System.out.println("-------- Oracle "
  35. + "JDBC Connection Testing ------------");
  36.  
  37. try {
  38.  
  39. Class.forName("oracle.jdbc.OracleDriver");
  40.  
  41. } catch (ClassNotFoundException e) {
  42.  
  43. System.out.println("Couldn't find Oracle JDBC Driver... :-(");
  44. e.printStackTrace();
  45. return;
  46.  
  47. }
  48.  
  49. System.out.println("Oracle JDBC Driver Registered!");
  50. myConnection = null;
  51. try {
  52. myConnection = DriverManager.getConnection(
  53. "jdbc:oracle:thin:@//"
  54. + host
  55. + ":"
  56. + port
  57. + "/"
  58. + dataBase,password
  59. );
  60.  
  61. } catch (sqlException e) {
  62.  
  63. System.out.println("Connection Failed!");
  64. e.printStackTrace();
  65. return;
  66.  
  67. }
  68.  
  69. if (myConnection != null) {
  70. System.out.println("Connected to Oracle! :-)");
  71. } else {
  72. System.out.println("Failed to make connection!");
  73. }
  74. }
  75.  
  76.  
  77. /**
  78. * Queries the database and returns a ResultSet
  79. * @param query sql
  80. * @throws sqlException
  81. */
  82. public ResultSet runQuery(String query) throws sqlException
  83. {
  84. System.out.println(" [DBConnection] Started Running @ " + (new SimpleDateFormat("HH:mm:ss:S")).format(new Date()));
  85. ResultSet rs = null;
  86.  
  87. Statement stt = myConnection.createStatement();
  88. rs = stt.executeQuery(query);
  89. System.out.println(" [DBConnection] Finished Running @: " + (new SimpleDateFormat("HH:mm:ss:S")).format(new Date()));
  90. return rs;
  91.  
  92. }

这是我得到的输出

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



..
.

解决方法

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

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

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

猜你在找的Java相关文章