Singleton Java类返回空值


春风助手
2025-03-21 01:38:17 (18小时前)

我创建了一个Java类,该类返回一个数据库连接对象。但是它在调用者方法处返回空值。当我在调试模式下运行代码时,它不会转到方法本身。需要建议。

  1. package com.cisco.installbase.hiveconnector;
  2. import java.sql.DriverManager;
  3. import java.sql.SQLException;
  4. import java.sql.Connection;
  5. public class CreateConnection {
  6. private static Connection instance = null;
  7. static final String drivername = "org.apache.hive.jdbc.HiveDriver";
  8. private CreateConnection() {
  9. try {
  10. Class.forName(drivername);
  11. // instance =
  12. // DriverManager.getConnection("jdbc:hive2://hddev-c01-edge-01:20000/",
  13. // "phodisvc", "B1GD4T4dev");
  14. // for hive 1 use this ------> instance =
  15. // DriverManager.getConnection("thrift://hddev-c01-edge-02:9083");
  16. instance = DriverManager.getConnection("thrift://hddev-c01-edge-02:9083");
  17. System.out.println("get instance"+instance);
  18. Constants.setFlag(true);
  19. } catch (ClassNotFoundException e) {
  20. e.printStackTrace();
  21. Constants.setFlag(false);
  22. } catch (SQLException e) {
  23. e.printStackTrace();
  24. Constants.setFlag(false);
  25. }
  26. }
  27. public static Connection getInstance() {
  28. Constants.setFlag(true);
  29. return instance;
  30. }
  31. }

下面是调用getInstance()方法的代码

  1. package com.cisco.installbase.hiveconnector;
  2. import java.util.Date;
  3. public class MainApp {
  4. private static final String hiveDB = ReadProperties.getInstance().getProperty("hive_db");
  5. private static final String logTable = ReadProperties.getInstance().getProperty("IB_log_table");
  6. private static final String dataGovernanceLogTable = ReadProperties.getInstance().getProperty("SR_DG_table");
  7. private static final String dataGovernanceMasterTable = ReadProperties.getInstance()
  8. .getProperty("SR_DG_master_table");
  9. private static final String count_xxccs_ds_sahdr_core = "select count(*) from " + hiveDB + "."
  10. + "xxccs_ds_sahdr_core";
  11. private static final String count_mtl_system_items_b = "select count(*) from " + hiveDB + "."
  12. + "mtl_system_items_b";
  13. private static final String count_xxccs_scdc_product_profile = "select count(*) from " + hiveDB + "."
  14. + "xxccs_scdc_product_profile";
  15. private static final String count_xxccs_ds_cvdprdline_detail = "select count(*) from " + hiveDB + "."
  16. + "xxccs_ds_cvdprdline_detail";
  17. private static final String count_xxccs_ds_instance_detail = "select count(*) from " + hiveDB + "."
  18. + "xxccs_ds_instance_detail";
  19. private static int currentJobID = 0;
  20. private static Date startTime = null;
  21. private static Date stopTime = null;
  22. private static int runTime = 0;
  23. static CommonDBUtilities commonDB = new CommonDBUtilities();
  24. static ShellUtilities shellUtilities = new ShellUtilities();
  25. static SqoopUtility sqoop = new SqoopUtility();
  26. public static void main(String[] args) {
  27. MainApp.startTimeLogger();
  28. System.out.println("Started the Job");
  29. }
  30. public static void startTimeLogger() {
  31. // getting the Job ID and the start time for the log table
  32. if (Constants.isFlag()) {
  33. currentJobID = commonDB.getMaximumJobID();
  34. startTime = commonDB.getTime();
  35. MainApp.importTables();
  36. System.out.println("executing startTimeLogger");
  37. } else {
  38. MainApp.onFailure();
  39. JobMailer.PostMail("IB Load Failed", "Load failed while logging method name startTimeLogger()");
  40. System.out.println("executing startTimeLogger failed");
  41. }
  42. }
  43. public static void importTables() {
  44. // Delete target directory before running the sqoop imports
  45. if (Constants.isFlag()) {
  46. shellUtilities.DeleteDirectory(Constants.getMtlSystems());
  47. shellUtilities.DeleteDirectory(Constants.getProductLine());
  48. shellUtilities.DeleteDirectory(Constants.getInstanceDetail());
  49. shellUtilities.DeleteDirectory(Constants.getProductLine());
  50. shellUtilities.DeleteDirectory(Constants.getHeaderCore());
  51. // Run the sqoop imports to load the data from oracle to hive
  52. sqoop.runSqoop();
  53. MainApp.getCounts();
  54. System.out.println("executing importTables");
  55. } else {
  56. MainApp.onFailure();
  57. JobMailer.PostMail("IB Load Failed", "Load failed while running sqoop import method name importTables()");
  58. System.out.println("executing importTables failed");
  59. }
  60. }
  61. public static void getCounts() {
  62. // Get the record counts for all the IB tables pulled
  63. if (Constants.isFlag()) {
  64. commonDB.getCounts(count_xxccs_ds_instance_detail);
  65. commonDB.getCounts(count_xxccs_ds_cvdprdline_detail);
  66. commonDB.getCounts(count_xxccs_scdc_product_profile);
  67. commonDB.getCounts(count_mtl_system_items_b);
  68. commonDB.getCounts(count_xxccs_ds_sahdr_core);
  69. MainApp.stopTimeLogger();
  70. System.out.println("executing getCounts");
  71. } else {
  72. MainApp.onFailure();
  73. JobMailer.PostMail("IB Load Failed", "Load failed while getting counts method name getCounts()");
  74. System.out.println("executing getCounts failed");
  75. }
  76. }
  77. public static void stopTimeLogger() {
  78. // Get the stop time or end time
  79. if (Constants.isFlag()) {
  80. stopTime = commonDB.getTime();
  81. MainApp.runTimeLogger();
  82. System.out.println("executing stopTimeLogger");
  83. } else {
  84. MainApp.onFailure();
  85. JobMailer.PostMail("IB Load Failed", "Load failed while end logging method name stopTimeLogger()");
  86. System.out.println("executing stopTimeLogger failed");
  87. }
  88. }
  89. public static void runTimeLogger() {
  90. // Get the run time or total time taken
  91. if (Constants.isFlag()) {
  92. runTime = (int) (stopTime.getTime() - startTime.getTime()) / 1000 * 60 * 60 * 24;
  93. MainApp.onSuccess();
  94. MainApp.logGovernance();
  95. System.out.println("executing runTimeLogger");
  96. } else {
  97. MainApp.onFailure();
  98. JobMailer.PostMail("IB Load Failed", "Load failed while runtime logging method name runTimeLogger()");
  99. System.out.println("executing runTimeLogger failed");
  100. }
  101. }
  102. public static void logGovernance() {
  103. // IB Data governance
  104. if (Constants.isFlag()) {
  105. String dataGovernance = "Insert into table " + hiveDB + "." + dataGovernanceLogTable
  106. + " select Data_Asset_Reference,File_Name,Origin_System,Transfer_System," + startTime
  107. + ",Column_Reference,Element_Reference,Rule_Priority,Delete_By_Date,Classification,Geographic_Inclusion,Geographic_Restriction,Group_Inclusion,Group_Restriction,Reserved from "
  108. + hiveDB + "." + dataGovernanceMasterTable;
  109. commonDB.InsertToTable(dataGovernance);
  110. System.out.println("executing logGovernance");
  111. } else {
  112. MainApp.onFailure();
  113. JobMailer.PostMail("IB Load Failed",
  114. "Load failed while inserting into datagovernance method name logGovernance()");
  115. System.out.println("executing logGovernance failed");
  116. }
  117. }
  118. public static void onFailure() {
  119. // Write to log on Failure
  120. String insertOnFailure = "insert into table " + hiveDB + "." + logTable + " select " + currentJobID + ","
  121. + stopTime + "," + runTime + "," + "FAILED from " + hiveDB + "." + "dual" + " limit 1; ";
  122. commonDB.InsertToTable(insertOnFailure);
  123. JobMailer.PostMail("IB Load Failed", "Load failed");
  124. System.out.println("executing onFailure");
  125. }
  126. public static void onSuccess() {
  127. // Write to log on Success
  128. String insertOnSuccess = "insert into table " + hiveDB + "." + logTable + " select " + currentJobID + ","
  129. + stopTime + "," + runTime + "," + "SUCCESS from " + hiveDB + "." + "dual" + " limit 1; ";
  130. commonDB.InsertToTable(insertOnSuccess);
  131. JobMailer.PostMail("IB Load Successfully completed", "Load completed");
  132. System.out.println("executing onSuccess");
  133. }
  134. }
2 条回复
  1. 1# 只怕再见是故人 | 2020-08-18 12-01

    您对sigleton的实现是错误的。您永远不会调用构造函数。这是实现单例的正确方法。

    1. public static Connection getInstance() {
    2. if (instance==null){
    3. instance = new CreateConnection();
    4. }
    5. Constants.setFlag(true);
    6. return instance;
    7. }
登录 后才能参与评论