我需要能够以每秒 5,000 次以上的写入速度写入我的数据库。目前,我无法管理超过 10% 的数字。
我正在使用 Spring 将我的数据源 (HSQL) 和 Hibernate 配置为我的 Jpa 提供程序。
我的实体都是扁平的(没有复杂的对象树),并且我使用了 Hibernate 二级缓存 (EhCache),并将并发策略设置为 ConcurrencyStrategy.READ-WRITE。
这是我的数据源 bean 的 Spring-Context:
<bean id="commandsTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="commandEmf"/>
<tx:annotation-driven transaction-manager="commandsTransactionManager" />
<!-- Commmands Data Source Configuration -->
<bean id="commandsDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc-commands.driverClassName}"/>
<property name="url" value="${jdbc-commands.url}"/>
<property name="username" value="${jdbc-commands.username}"/>
<property name="password" value="${jdbc-commands.password}"/>
<property name="initialSize" value="10"/>
<property name="maxActive" value="100"/>
<property name="maxWait" value="-1"/>
</bean>
<bean name="lazyConnectionDataSourceProxy" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
<property name="targetDataSource" ref="commandsDataSource" />
</bean>
<!-- Commands Container Managed JPA Entity Manager Factory -->
<bean id="commandEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="commandsDataSource"/>
<property name="persistenceUnitName" value="commands"/>
<property name="jpaPropertyMap" ref="jpaPropertyMap"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
p:showSql="false" p:generateDdl="true" p:database="HSQL" p:databasePlatform="org.hibernate.dialect.HSQLDialect">
</bean>
</property>
</bean>
<util:map id="jpaPropertyMap" key-type="java.lang.String" value-type="java.lang.Object">
<entry key="hibernate.hbm2ddl.auto" value="${jdbc-commands.ddlmode}" />
<entry key="hibernate.cache.use_second_level_cache" value="true" />
<entry key="hibernate.cache.region.factory_class" value="net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory"/>
</util:map>
这是我的一个实体类的示例。“AbstractAnnotatedAggregateRoot”是来自Axon 框架的抽象实体类,它提供聚合和存储库实现框架。聚合本质上是必须实现自己的 Jpa 以实现持久性的实体。
@Entity
@Table(name = "users")
@Cache(region="usersCache", usage= CacheConcurrencyStrategy.READ_WRITE)
public class User extends AbstractAnnotatedAggregateRoot {
private static final long serialVersionUID = -6536766172448063298L;
private String username;
private String password;
private Integer subscription;
private String firstName;
private String lastName;
private Calendar subscriptionDate;
private Date lastAccessTime;
public User(){}
public User(StringAggregateIdentifier email){
super(email);
registerEvent(new UserCreatedEvent(email.asString()));
}
@Column(unique = true)
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
registerEvent(new UserNameUpdatedEvent(this.username));
}
@Column(nullable = false)
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
registerEvent(new UserPasswordUpdatedEvent(this.password));
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
registerEvent(new UserFirstNameUpdatedEvent(this.firstName));
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
registerEvent(new UserLastNameUpdatedEvent(this.lastName));
}
public String getEmail() {
return getIdentifier().asString();
}
@Temporal(TemporalType.DATE)
public Calendar getSubscriptionDate() {
return subscriptionDate;
}
public void setSubscriptionDate(Calendar subscriptionDate) {
this.subscriptionDate = subscriptionDate;
registerEvent(new UserSubscriptionDateUpdatedEvent(this.subscriptionDate));
}
@Temporal(TemporalType.TIMESTAMP)
public Date getLastAccessTime() {
return lastAccessTime;
}
public void setLastAccessTime(Date lastAccessTime) {
this.lastAccessTime = lastAccessTime;
registerEvent(new UserLastAccessTimeUpdatedEvent(this.lastAccessTime));
}
public Integer getSubscription() {
return subscription;
}
public void setSubscription(Integer subscription) {
this.subscription = subscription;
registerEvent(new UserSubscriptionUpdatedEvent(this.subscription));
}
}
为了完整起见,这里是我的 EhCache.xml 文件中的缓存区域配置:
<cache name="usersCache" maxElementsInMemory="10000"
maxElementsOnDisk="10000" eternal="false" overflowToDisk="false"
diskSpoolBufferSizeMB="20" timeToIdleSeconds="300"
timeToLiveSeconds="600" memoryStoreEvictionPolicy="LFU"
statistics = "true">
</cache>
在 Axon 框架内,我调度命令来尝试更改聚合的状态。这些命令在由 Spring 的 PlatformTransactionManager 管理的事务的范围内。
命令调度同步发生,我需要每秒处理 5,000 个命令。每个命令将从缓存中获取聚合,更改其上的值,然后在调度下一个命令之前保持更改。问题在于获取/更新/插入行所花费的时间,考虑到 HYSQL 和 H2 之类的 INSERT 速率,我认为这会快得多。
任何将速度提高一个数量级的想法将不胜感激。
谢谢