2014-04-30 39 views
0

我有一个类名为:ReceiptGenerateAgentDAO receiptGenerateAgentDAO = new ReceiptGenerateAgentDAO();这个类将从jsp 页面中调用,该页面有一个方法,并且我有一个for循环,它将执行多于5分钟并且问题是我请求jsp到 调用类并且类开始执行,现在我重新加载jsp 并再次调用该类,现在这两个类都在运行,它不是 线程,它如何可能以及如何解决它?如何避免类java的同一个实例正在运行多次

for (String Key : list) { 
     if (!"userDetailsCache".equals(Key)) { 
      preparedStatement2.setString(1, Key); 
      @Cleanup 
      ResultSet rsCreditReceipt = preparedStatement2.executeQuery(); 
      if (rsCreditReceipt.next() 
        && rsCreditReceipt.getString("SUMMARY_DATE") != null) { 
       String exBalance = rsCreditReceipt.getString("balance"); 
       /** Agent Current Balance */ 
       preparedStatement3.setString(1, 
         rsCreditReceipt.getString("SUMMARY_DATE")); 
       preparedStatement3.setString(2, Key); 
       @Cleanup 
       ResultSet rsCreditBal = preparedStatement3.executeQuery(); 
       double creditDebit = Double.parseDouble(exBalance); 
       if (rsCreditBal.next()) { 
        if (rsCreditBal.getString("creditdebit") != null) { 
         creditDebit += Double.parseDouble(rsCreditBal 
           .getString("creditdebit")); 
        } 
       } 
       if (creditDebit < 0) { 
        Element element = EhcacheManager.getUserDetailsCache() 
          .get(Key); 
        UserDetails details = (UserDetails) element 
          .getObjectValue(); 
        /** Agent Opening Balance */ 
        getZoneDetails(details, connection); 
        receiptGenerateTempDTOs.add(new ReceiptGenerateTempDTO(
          details.getTerritoryId(), details 
            .getSatelliteId(), details.getZoneId(), 
          rsCreditReceipt.getString(3), 
          NumericConstants.ZERO, String.valueOf(formatter 
            .format((creditDebit * (-1)))), time, 
          receiptGenerateAgentDTO.getSelectedUserId(), 
          NumericConstants.ZERO)); 
       } 
      } else { 
       /** If no opening balance,only current Balance */ 
       preparedStatement4.setString(1, Key); 
       @Cleanup 
       ResultSet rsCreditBal = preparedStatement4.executeQuery(); 
       if (rsCreditBal.next()) { 
        Element element = EhcacheManager.getUserDetailsCache() 
          .get(Key); 
        UserDetails details = (UserDetails) element 
          .getObjectValue(); 
        getZoneDetails(details, connection); 
        receiptGenerateTempDTOs 
          .add(new ReceiptGenerateTempDTO(
            details.getTerritoryId(), 
            details.getSatelliteId(), 
            details.getZoneId(), 
            details.getUserId(), 
            NumericConstants.ZERO, 
            String.valueOf(formatter.format((Double.parseDouble(rsCreditBal 
              .getString("creditdebit")) * (-1)))), 
            time, receiptGenerateAgentDTO 
              .getSelectedUserId(), 
            NumericConstants.ZERO)); 
       } 
      } 
     } 
     System.out.println(i++); 
    } 

+0

所以你需要把它作为'Singleton'类来避免多次实例化它。 –

+0

你能告诉我们一些代码吗? – Keerthivasan

+0

我用代码 – sanjaykumar

回答

0

你可以使用Singleton模式创建一个类,将只有一个实例:

public class Singleton { 
private static Singleton instance = null; 

private Singleton() { 
    // Is private to be not accessible 
} 

public static Singleton getInstance() { 
    if (instance == null) { 
     instance = new Singleton(); 
    } 
    return instance; 
} 
} 

的,如果你想调用的单例类,使用方法:Singleton.getInstance.someMethod()

+0

Akkusativobjekt,但它是一个普通的类,如果是的话我想使用所有类都有singleton – sanjaykumar

+0

只需用访问模式包装你的类。 – Akkusativobjekt

+0

如果它已经在运行,它可能关闭那个类的实例 – sanjaykumar

0

可以通过多种方式来解决..

方法1:

更改类会话范围的范围。 由此你的班级将不会在每次请求后收集垃圾。 Class对象将保留到会话过期。


方法2:

更改类单例的范围。通过这个,你将为整个用户会话创建一个对象。你可以在这些步骤中做到这一点。

步骤1: 创建的类的一个静态对象,并使用一个空值初始化它

Private Static ClassName staticClassName=null; 

步骤2: 块构造器,这样的ClassName OBJ =新类名()不会给你一个新的对象。

private ClassName() {....} 

步骤3 创建另一个方法findClassObject()的返回类

public static ClassName findClassObject(){ 
if(staticClassName==null){ 
staticClassName= new ClassName() ; //since the constructor method is called from within the class, it returns a new Object of the Class ClassName 
} 
return staticClassName; 
} 

的对象SETP 4

使用这种初始化类对象,该对象总是返回相同的对象。

ClassName obj= ClassName.findClassObject(); 
相关问题