2013-12-11 63 views
0

当我将应用程序移至生产时,应该怎么做。 Spring + Jersey + Hibernate的应用程序。静态方法还是非静态方法?

预期的服务器点击次数/秒 - 40到50 .. Serverhits /分钟 - 1000到1500。这个服务器负载应该怎么我的配置是什么?

Tomcat的 在生产过程中是确定的,如果我设置这些为Tomcat

初始JVM堆大小:256米 最大JVM堆大小:256米 最大JVM持久代的大小:64M

如果不是请建议。 P.S:我将它托管在自动缩放的云实例中。所以RAM或CPU没有问题。

春 答:DB操作静态架构。

public class Booking { 

     @Autowired // this is autowired by spring 
     private SessionFactory sessionFactory; // Hibernate class 

     @POST // this is Jersey (I guess this can handle 50 requests per soconds) 
     @Path("/create") 
     @Produces("application/json") 
     public Response create(String json) { 
       .... 
     DBHelper.insertIntoDb(booking, sessionFactory); // here i use static architecture. The reason why i used this is i dont want new object created for each request. (I tested with 10 request per seconds.. Will this be able to handle 50 request per second... or even 500 requests per second) 
       .... 
       return Response.ok(container).build(); 
     } 
} 

public class DBHelper { 
     /** 
    * Inserts the object in DB 
    * @param object 
    * @param sessionFactory 
    * @return the saved object 
    * @throws Exception 
    * @throws ConstraintViolationException 
    */ 
    public static Object insertIntoDb(Object object, SessionFactory sessionFactory) throws Exception, ConstraintViolationException { 
     synchronized (sessionFactory) { 
      Session session = sessionFactory.openSession(); 
      Transaction transaction = null; 
      try { 
       transaction = session.beginTransaction(); 
       if (object != null) { 
        session.save(object); 
        session.flush(); 
        session.clear(); 
        transaction.commit(); 
        return object; 
       } 
      } catch (ConstraintViolationException e) { 
       transaction.rollback(); 
       throw new ConstraintViolationException(e.toString(), null, null, null); 
      } catch (Exception e) { 
       transaction.rollback(); 
       throw new Exception(e); 
      } finally { 
       session.close(); 
      } 
     } 
     return object; 
    } 
} 

B:对DB操作非静态架构。

public class Booking { 

     @Autowired // this is autowired by spring 
     private SessionFactory sessionFactory; // Hibernate class 

     @POST // this is Jersey (I guess this can handle 50 requests per soconds) 
     @Path("/create") 
     @Produces("application/json") 
     public Response create(String json) { 
       .... 
     new DBHelper().insertIntoDb(booking, sessionFactory); // Non static 
       .... 
       return Response.ok(container).build(); 
     } 
} 

public class DBHelper { 
     /** 
    * Inserts the object in DB 
    * @param object 
    * @param sessionFactory 
    * @return the saved object 
    * @throws Exception 
    * @throws ConstraintViolationException 
    */ 
    public Object insertIntoDb(Object object, SessionFactory sessionFactory) throws Exception, ConstraintViolationException { 
     synchronized (sessionFactory) { 
      Session session = sessionFactory.openSession(); 
      Transaction transaction = null; 
      try { 
       transaction = session.beginTransaction(); 
       if (object != null) { 
        session.save(object); 
        session.flush(); 
        session.clear(); 
        transaction.commit(); 
        return object; 
       } 
      } catch (ConstraintViolationException e) { 
       transaction.rollback(); 
       throw new ConstraintViolationException(e.toString(), null, null, null); 
      } catch (Exception e) { 
       transaction.rollback(); 
       throw new Exception(e); 
      } finally { 
       session.close(); 
      } 
     } 
     return object; 
    } 
} 

答:静态架构 优点: 1)我不是创建单独的对象 2)因此,确保我的Java堆未填充 3)工作少垃圾收集器 缺点: 1)错了对象可以从insertIntoDd方法返回...(我想这...但没有面对测试一个单一的问题)。

B:非 - 静态架构 优点: 1)无疑是正确的数据将被从insertIntoDd方法 缺点返回: 1)我创建单个对象 2)Java堆可能会导致OutOfMemoryException异常 3)需要更多工作以垃圾收集器

我完全糊涂了。

我该怎么办?

回答

0

我的建议是将单一模式用于您的数据库帮助程序。因此,您将在单个DBHelper类中具有sessionFactory,并且它将在该类内部使用private sessionFactory变量。这更类似于你的静态方法,但使用适当的模式。

对于内存使用情况(更好地以250 MB最小值和最大1024 MB,Perm为256 MB开始)。稍后,您可以随着负载的增加而增加内存。

例如如下..

public final class DBHelper { 

私有会话工厂的sessionFactory;

private static DBHelper instance = null;

//私有构造

私人DBHelper(){

//这里初始化会话工厂

}

公共静态DBHelper的getInstance(){

如果(实例== null){

instance = new DBHelper();

}

return instance;

}

公共静态对象insertIntoDb(Object对象)抛出异常,ConstraintViolationException {

//做你的代码中插入DB

}

}

公共类预订{

//所以总是使用单个insertDb方法

DBHelper.getInstance()。insertIntoDb(booking);

...

}

0

如果你没有创建大量的对象,那么去非静态的唯一缺点就是你使用的额外对象的开销为12字节,唯一真正的好处是能够轻松修改它使服务的许多独立实例。

如果你想有服务的多个实例,而无需启动多一点的JVM,我非静态去。否则,这可能没有关系。