【发布时间】:2013-12-11 05:18:38
【问题描述】:
当我将我的应用程序转移到生产环境时我应该怎么做。 Spring + Jersey + Hibernate 的应用程序。
Expected Server hits/second - 40 到 50.. Serverhits/min - 1000 到 1500。对于此服务器负载,我的配置应该如何?
汤姆猫 在生产过程中,如果我为tomcat设置这些就可以了
初始JVM堆大小:256m 最大JVM堆大小:256m 最大JVM永久代大小:64m
如果不是请提出建议。 P.S:我将它托管在自动缩放的云实例中。所以 RAM 或 CPU 都没有问题。
春天 A:数据库操作的静态架构。
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:用于数据库操作的非静态架构。
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;
}
}
A:静态架构 优点: 1)我没有创建单个对象 2)所以确保我的java堆没有填满 3) 减少垃圾收集器的工作 缺点: 1) insertIntoDd 方法可能返回错误的对象...(我猜是这个...但在测试中没有遇到任何问题)。
B:非静态架构 优点: 1) insertIntoDd 方法肯定会返回正确的数据 缺点: 1)我正在创建单个对象 2)Java堆可能会导致OutOfMemoryException 3) 垃圾收集器的更多工作
我完全糊涂了。
我该怎么办??
【问题讨论】:
标签: java spring hibernate tomcat jersey