日期:2014-05-16  浏览次数:20364 次

Hibernate使用自定义主键生成器@TableGenerator和数据库自身生成器的性能测试
Hibernate的主键生成策略与并发测试http://blog.sina.com.cn/s/blog_56d8ea900100xb8h.html
一些重要的知识,可以在这里看到.

SpringMVC3.x, Hiberante4, sql server 2008, 数据库的VM内存2G, 本机的web环境内存8G(VM在本机上建立的), j2ee环境option内存:1G.  服务器:jetty,  idea采用非调试启动.
这里不想讨论为什么使用@TableGenerator, 因为没必要,只是比较性能而已.

在运行是, 没有手动session.flush()等操作.

使用自定义主键生成器的service
pojo的主键定义示例:
@GeneratedValue(strategy = GenerationType.TABLE,generator="customer_gen")
    @TableGenerator(name = "customer_gen",
            table="tb_generator",
            pkColumnName="gen_name",
            valueColumnName="gen_value",
            pkColumnValue="testtable_PK"
    )

注意,没有设定allocationSize属性,默认是50.
@Override
    public void saveTestData(int sum) {
        /*
        DELETE FROM dbo.testtable3;
        DELETE FROM dbo.testtable2;
        DELETE FROM dbo.testtable1;
        DELETE FROM dbo.testtable;
         */
        Date d1 = new Date();
        //单表
        for(int i=0; i<sum; i++){
            Testtable t = new Testtable();
            t.setCode("Test"+i);
            testtableDao.save(t);
            //System.out.println("自定义主键生成 记录:"+i+" 单表:"+(new Date()).toString());
        }
        System.out.println("完成单表操作");


        //关联
        for(int i=0; i<sum; i++){
            Testtable1 p = new Testtable1();
            p.setCode("P"+i);
            p = testtable1Dao.save(p);

            Testtable2 c = new Testtable2();
            c.setTesttable1(p);
            c.setCode("C"+i);
            testtable2Dao.save(c);
            //System.out.println("自定义主键生成 记录:"+i+" 关联:"+(new Date()).toString());
        }
        System.out.println("完成关联操作");

        //映射
        for(int i=0;i<sum; i++){
            Testtable t = new Testtable();
            t.setCode("T"+i);
            t =  testtableDao.save(t);

            Testtable2 c = new Testtable2();
            c.setCode("C"+i);
            c = testtable2Dao.save(c);

            Testtable3 m = new Testtable3();
            m.setCode("M"+i);
            m.setTesttable(t);
            m.setTesttable2(c);
            m = testtable3Dao.save(m);
            //System.out.println("自定义主键生成 记录:"+i+" 映射:"+(new Date()).toString());
        }
        System.out.println("完成映射操作");
        System.out.println("Over......");
        Date d2 = new Date();
        long t = d2.getTime() - d1.getTime();
        System.out.println("service耗时=" + (t / 1000) + "s");

    }


使用数据库自己的主键生成器的service
pojo的主键定义示例:
@GeneratedValue(strategy = GenerationType.AUTO)

@Override
    public void saveTestData(int sum) {
        /*
        DELETE FROM dbo.atabletest3;
        DELETE FROM dbo.atabletest2;
        DELETE FROM dbo.atabletest1;
        DELETE FROM dbo.atabletest;
         */
        Date d1 = new Date();
        //单表
        for(int i=0; i<sum; i++){
            Atabletest t = new Atabletest();
            t.setCode("Test"+i);
            atabletestDao.save(t);
            //System.out.println("数据库主键生成 记录:"+i+" 单表:"+(new Date()).toString());
        }
        System.out.println("完成单表操作");



        //关联
        for(int i=0; i<sum; i++){
            Atabletest1 p = new Atabletest1();
            p.setCode("P"+i);
            p = atabletest1Dao.save(p);

            Atabletest2 c = new Atabletest2();
            c.setAtabletest1(p);
            c.setCode("C"+i);
            atabletest2Dao.save(c);
            //System.out.println("数据库主键生成 记录:"+i+" 关联:"+(new Date()).toString());
        }
        System.out.println("完成关联操作");


        //映射
        for(int i=0;i<sum; i++){
            Atabletest t = new Atabletest();
            t.setCode("T"+i);
            t =  atabletestDao.save(t);

            Atabletest2 c = new Atabletest2();
            c.setCode("C"+i);
            c