Hibernate批处理在EJB / JPA项目中不起作用

时间:2019-04-04 17:22:54

标签: hibernate-batch-updates

我正在尝试使用部署在Wildfly应用程序服务器中的EJB来使休眠批处理工作,并利用postgres进行持久化。

无论我使用什么配置以及创建更新的方式多么简单,休眠都将批处理插入。我正在使用:

  • Java 8
  • Wildfly 10.1(Hibernate 5.0.10.Final)
  • Postgres 9.6(PostgreSQL JDBC驱动程序42.2.5)

我创建了一个示例来保留一个相当简单的对象的多行。我在现有的无状态会话bean中使用一种方法来运行示例。

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
   xmlns="http://java.sun.com/xml/ns/persistence" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence">

    <persistence-unit name="bpcPAS">
        <jta-data-source>java:/jdbc/DB_bpcPAS</jta-data-source>
        <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
        <properties>
            <property name="jboss.entity.manager.factory.jndi.name" value="persistence-units/bpcPAS" />
            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.connection.isolation" value="2" />
            <property name="hibernate.jdbc.batch_size" value="20" />
            <property name="hibernate.order_inserts" value="true" />
            <property name="hibernate.order_updates" value="true" />
            <property name="hibernate.default_schema" value="pas"/>
            <property name="hibernate.cache.use_second_level_cache" value="true"/>
        </properties>
    </persistence-unit>
</persistence>

简单的 FxRate 实体bean:

public class FxRate implements Serializable {

    @Embeddable
    public static class FxRatePK implements Serializable {

        /**
         * Currency Type
         */
        @Enumerated(EnumType.STRING)
        @Column(name="CURRENCY_TYPE")
        private CurrencyType currencyType = null;

        /**
         * Price date
         */
        @Column(name="PRICE_DATE")
        @org.hibernate.annotations.Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalDate")
        private LocalDate priceDate;

        public FxRatePK() {}

        public FxRatePK(CurrencyType currencyType, LocalDate priceDate) {
            super();
            this.currencyType = currencyType;
            this.priceDate = priceDate;
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((currencyType == null) ? 0 : currencyType.hashCode());
            result = prime * result + ((priceDate == null) ? 0 : priceDate.hashCode());
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            FxRatePK other = (FxRatePK) obj;
            if (currencyType != other.currencyType)
                return false;
            if (priceDate == null) {
                if (other.priceDate != null)
                    return false;
            } else if (!priceDate.equals(other.priceDate))
                return false;
            return true;
        }

        public CurrencyType getCurrencyType() {
            return currencyType;
        }

        public void setCurrencyType(CurrencyType currencyType) {
            this.currencyType = currencyType;
        }

        public LocalDate getPriceDate() {
            return priceDate;
        }

        public void setPriceDate(LocalDate priceDate) {
            this.priceDate = priceDate;
        }
    }

    /**
     * Primary key
     */
    @EmbeddedId
    protected FxRatePK pk;

    /**
     * Price 
     */
    @Column(name="PRICE", precision=15, scale=10)
    @Type(type = "com.silver.bpc.common.hibernate.NumericType")
    private Double price;

    /**
     * Default Constructor
     */
    public FxRate() {}

    /**
     * Constructor
     * @param currType
     * @param priceDate
     * @param price
     */
    public FxRate(CurrencyType currType, LocalDate priceDate, Double price) {
        this.pk = new FxRatePK(currType, priceDate);
        this.price = price;
    }

    public CurrencyType getCurrencyType() {
        return pk.currencyType;
    }

    public LocalDate getPriceDate() {
        return pk.priceDate;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }
}

正在运行示例代码(使用 bpcPAS 实体管理器“ em”):

LocalDate date = new LocalDate();

for (int i=0; i<200; i++) {
    FxRate fxRate = new FxRate(CurrencyType.EUR, date.minusDays(i), 1.0);
    em.persist(fxRate);
}
em.flush();
em.clear();

在此示例中,休眠应创建10条插入语句,每条插入20行。调试信息表示,它正在执行20的批处理大小,但是输出到日志的实际语句表示相反。使用不同的批处理大小完成的计时会产生相同的结果,表明一次发送了一次插入(如日志所示):

2019-04-04 13:00:12,608 DEBUG [org.hibernate.SQL] (default task-6) insert into pas.FX_RATE (PRICE, CURRENCY_TYPE, PRICE_DATE) values (?, ?, ?)
2019-04-04 13:00:12,615 DEBUG [org.hibernate.SQL] (default task-6) insert into pas.FX_RATE (PRICE, CURRENCY_TYPE, PRICE_DATE) values (?, ?, ?)
2019-04-04 13:00:12,616 DEBUG [org.hibernate.SQL] (default task-6) insert into pas.FX_RATE (PRICE, CURRENCY_TYPE, PRICE_DATE) values (?, ?, ?)
2019-04-04 13:00:12,618 DEBUG [org.hibernate.SQL] (default task-6) insert into pas.FX_RATE (PRICE, CURRENCY_TYPE, PRICE_DATE) values (?, ?, ?)
2019-04-04 13:00:12,619 DEBUG [org.hibernate.SQL] (default task-6) insert into pas.FX_RATE (PRICE, CURRENCY_TYPE, PRICE_DATE) values (?, ?, ?)
2019-04-04 13:00:12,621 DEBUG [org.hibernate.SQL] (default task-6) insert into pas.FX_RATE (PRICE, CURRENCY_TYPE, PRICE_DATE) values (?, ?, ?)
2019-04-04 13:00:12,622 DEBUG [org.hibernate.SQL] (default task-6) insert into pas.FX_RATE (PRICE, CURRENCY_TYPE, PRICE_DATE) values (?, ?, ?)
2019-04-04 13:00:12,624 DEBUG [org.hibernate.SQL] (default task-6) insert into pas.FX_RATE (PRICE, CURRENCY_TYPE, PRICE_DATE) values (?, ?, ?)
2019-04-04 13:00:12,626 DEBUG [org.hibernate.SQL] (default task-6) insert into pas.FX_RATE (PRICE, CURRENCY_TYPE, PRICE_DATE) values (?, ?, ?)
2019-04-04 13:00:12,627 DEBUG [org.hibernate.SQL] (default task-6) insert into pas.FX_RATE (PRICE, CURRENCY_TYPE, PRICE_DATE) values (?, ?, ?)
2019-04-04 13:00:12,629 DEBUG [org.hibernate.SQL] (default task-6) insert into pas.FX_RATE (PRICE, CURRENCY_TYPE, PRICE_DATE) values (?, ?, ?)
2019-04-04 13:00:12,630 DEBUG [org.hibernate.SQL] (default task-6) insert into pas.FX_RATE (PRICE, CURRENCY_TYPE, PRICE_DATE) values (?, ?, ?)
2019-04-04 13:00:12,632 DEBUG [org.hibernate.SQL] (default task-6) insert into pas.FX_RATE (PRICE, CURRENCY_TYPE, PRICE_DATE) values (?, ?, ?)
2019-04-04 13:00:12,634 DEBUG [org.hibernate.SQL] (default task-6) insert into pas.FX_RATE (PRICE, CURRENCY_TYPE, PRICE_DATE) values (?, ?, ?)
2019-04-04 13:00:12,636 DEBUG [org.hibernate.SQL] (default task-6) insert into pas.FX_RATE (PRICE, CURRENCY_TYPE, PRICE_DATE) values (?, ?, ?)
2019-04-04 13:00:12,637 DEBUG [org.hibernate.SQL] (default task-6) insert into pas.FX_RATE (PRICE, CURRENCY_TYPE, PRICE_DATE) values (?, ?, ?)
2019-04-04 13:00:12,639 DEBUG [org.hibernate.SQL] (default task-6) insert into pas.FX_RATE (PRICE, CURRENCY_TYPE, PRICE_DATE) values (?, ?, ?)
2019-04-04 13:00:12,640 DEBUG [org.hibernate.SQL] (default task-6) insert into pas.FX_RATE (PRICE, CURRENCY_TYPE, PRICE_DATE) values (?, ?, ?)
2019-04-04 13:00:12,642 DEBUG [org.hibernate.SQL] (default task-6) insert into pas.FX_RATE (PRICE, CURRENCY_TYPE, PRICE_DATE) values (?, ?, ?)
2019-04-04 13:00:12,643 DEBUG [org.hibernate.SQL] (default task-6) insert into pas.FX_RATE (PRICE, CURRENCY_TYPE, PRICE_DATE) values (?, ?, ?)
2019-04-04 13:00:12,645 DEBUG [org.hibernate.engine.jdbc.batch.internal.BatchingBatch] (default task-6) Executing batch size: 20
2019-04-04 13:00:12,648 DEBUG [org.hibernate.SQL] (default task-6) insert into pas.FX_RATE (PRICE, CURRENCY_TYPE, PRICE_DATE) values (?, ?, ?)
2019-04-04 13:00:12,650 DEBUG [org.hibernate.SQL] (default task-6) insert into pas.FX_RATE (PRICE, CURRENCY_TYPE, PRICE_DATE) values (?, ?, ?)
2019-04-04 13:00:12,651 DEBUG [org.hibernate.SQL] (default task-6) insert into pas.FX_RATE (PRICE, CURRENCY_TYPE, PRICE_DATE) values (?, ?, ?)
2019-04-04 13:00:12,653 DEBUG [org.hibernate.SQL] (default task-6) insert into pas.FX_RATE (PRICE, CURRENCY_TYPE, PRICE_DATE) values (?, ?, ?)
2019-04-04 13:00:12,654 DEBUG [org.hibernate.SQL] (default task-6) insert into pas.FX_RATE (PRICE, CURRENCY_TYPE, PRICE_DATE) values (?, ?, ?)
2019-04-04 13:00:12,656 DEBUG [org.hibernate.SQL] (default task-6) insert into pas.FX_RATE (PRICE, CURRENCY_TYPE, PRICE_DATE) values (?, ?, ?)
2019-04-04 13:00:12,657 DEBUG [org.hibernate.SQL] (default task-6) insert into pas.FX_RATE (PRICE, CURRENCY_TYPE, PRICE_DATE) values (?, ?, ?)
2019-04-04 13:00:12,659 DEBUG [org.hibernate.SQL] (default task-6) insert into pas.FX_RATE (PRICE, CURRENCY_TYPE, PRICE_DATE) values (?, ?, ?)
2019-04-04 13:00:12,660 DEBUG [org.hibernate.SQL] (default task-6) insert into pas.FX_RATE (PRICE, CURRENCY_TYPE, PRICE_DATE) values (?, ?, ?)
2019-04-04 13:00:12,662 DEBUG [org.hibernate.SQL] (default task-6) insert into pas.FX_RATE (PRICE, CURRENCY_TYPE, PRICE_DATE) values (?, ?, ?)
2019-04-04 13:00:12,664 DEBUG [org.hibernate.SQL] (default task-6) insert into pas.FX_RATE (PRICE, CURRENCY_TYPE, PRICE_DATE) values (?, ?, ?)
2019-04-04 13:00:12,665 DEBUG [org.hibernate.SQL] (default task-6) insert into pas.FX_RATE (PRICE, CURRENCY_TYPE, PRICE_DATE) values (?, ?, ?)
2019-04-04 13:00:12,667 DEBUG [org.hibernate.SQL] (default task-6) insert into pas.FX_RATE (PRICE, CURRENCY_TYPE, PRICE_DATE) values (?, ?, ?)
2019-04-04 13:00:12,668 DEBUG [org.hibernate.SQL] (default task-6) insert into pas.FX_RATE (PRICE, CURRENCY_TYPE, PRICE_DATE) values (?, ?, ?)
2019-04-04 13:00:12,670 DEBUG [org.hibernate.SQL] (default task-6) insert into pas.FX_RATE (PRICE, CURRENCY_TYPE, PRICE_DATE) values (?, ?, ?)
2019-04-04 13:00:12,671 DEBUG [org.hibernate.SQL] (default task-6) insert into pas.FX_RATE (PRICE, CURRENCY_TYPE, PRICE_DATE) values (?, ?, ?)
2019-04-04 13:00:12,673 DEBUG [org.hibernate.SQL] (default task-6) insert into pas.FX_RATE (PRICE, CURRENCY_TYPE, PRICE_DATE) values (?, ?, ?)
2019-04-04 13:00:12,674 DEBUG [org.hibernate.SQL] (default task-6) insert into pas.FX_RATE (PRICE, CURRENCY_TYPE, PRICE_DATE) values (?, ?, ?)
2019-04-04 13:00:12,675 DEBUG [org.hibernate.SQL] (default task-6) insert into pas.FX_RATE (PRICE, CURRENCY_TYPE, PRICE_DATE) values (?, ?, ?)
2019-04-04 13:00:12,677 DEBUG [org.hibernate.SQL] (default task-6) insert into pas.FX_RATE (PRICE, CURRENCY_TYPE, PRICE_DATE) values (?, ?, ?)
2019-04-04 13:00:12,678 DEBUG [org.hibernate.engine.jdbc.batch.internal.BatchingBatch] (default task-6) Executing batch size: 20
...

感谢您可以提供的任何帮助!

0 个答案:

没有答案