【问题标题】:Static approach or non static approach?静态方法还是非静态方法?
【发布时间】: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


    【解决方案1】:

    我的建议是为您的数据库助手使用单一模式。因此,您将在单个 DBHelper 类中拥有 sessionFactory,并且它将在类中私有 sessionFactory 变量。这更类似于您的静态方法,但使用了正确的模式。

    对于内存使用(最好从最小 250 MB 和最大 1024 MB 开始,Perm 为 256MB)。后者可以随着负载的增加而增加内存。

    例如如下..

    public final class DBHelper {
    

    私有 SessionFactory sessionFactory;

    私有静态 DBHelper 实例=null;

    // 私有构造函数

    私有 DBHelper() {

    //在这里初始化你的会话工厂

    }

    公共静态 DBHelper getInstance() {

    如果(实例 == null){

    instance = new DBHelper();

    }

    返回实例;

    }

    public static Object insertIntoDb(Object object) 抛出异常,ConstraintViolationException {

    //编写代码以插入数据库

    }

    }

    公开课预订{

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

    DBHelper.getInstance().insertIntoDb(booking);

    ...

    }

    【讨论】:

      【解决方案2】:

      如果您不创建大量对象,则非静态的唯一缺点是您使用的 ONE 额外对象的开销约为 12 字节,唯一真正的优点是能够轻松地将其修改为制作大量独立的服务实例。

      如果您想要拥有多个服务实例而不必启动更多的小型 JVM,我会选择非静态的。否则,可能没关系。

      【讨论】:

        猜你喜欢
        • 2012-06-17
        • 2011-02-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多