當我將應用程序移至生產時,應該怎麼做。 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)需要更多工作以垃圾收集器
我完全糊塗了。
我該怎麼辦?