我有一个非常简单的spring-data-jpa + Hibernate应用程序,它将客户数据存储在MySql数据库中(完整的源代码发布here)。
问题是它运行 insert 和选择查询非常慢,与我通过mysql CLI进行比较:
有一个类似的问题被讨论here,但在我的情况下,测量结果更糟糕(即使我不插入批处理,它只是DB中的一个简单行)。有没有办法提高Spring JPA / Hibernate的性能?
另一个问题是,有没有办法减少spring-data-jpa和hibernate-entitymanager的大小?我能够在不损害程序的情况下排除byte-buddy和jandex依赖项,但是只有几个Mbs(阴影jar大小从19.6Mb下降到16.6Mb)?
更新 根据请求,这里是代码(所有来源都是here):
@Entity
@AttributeAccessor("field")
@Table(name = "customer")
public class Customer implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id",unique=true, nullable=false, insertable=true, updatable=true)
@Type(type="long")
private Long id;
@Column(name = "name")
private String name;
//+ constructors, getters/setters
}
这是Spring应用程序,也是保存(插入)客户:
public class Application {
private static ApplicationContext applicationContext;
static CustomerRepository customerRepository;
public static void main(String[] args) throws InterruptedException {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MySQLAutoconfiguration.class);
customerRepository = ctx.getBean(CustomerRepository.class);
runTest();
}
private static void runTest () throws {
...
//insert
Customer customerJohn = customerRepository.save(new Customer("John"));
//select
Customer foundEntity = customerRepository.findOne(customerJohn.getId());
...
}
配置:
@Configuration
@ComponentScan
@EnableJpaRepositories (basePackages = "com.vk.dal.repository")
@PropertySource("classpath:mysql.properties")
public class MySQLAutoconfiguration {
@Autowired
private Environment env;
@Bean
public DataSource dataSource() {
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
dataSource.setUrl(env.getProperty("spring.datasource.url"));
dataSource.setUsername(env.getProperty("spring.datasource.username") != null ? env.getProperty("spring.datasource.username") : "");
dataSource.setPassword(env.getProperty("spring.datasource.password") != null ? env.getProperty("spring.datasource.password") : "");
return dataSource;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource);
em.setPackagesToScan("com.vk.dal.domain");
em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
Properties properties = additionalProperties();
if (properties != null) {
em.setJpaProperties(properties);
}
return em;
}
@Bean
JpaTransactionManager transactionManager(final EntityManagerFactory entityManagerFactory) {
final JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory);
return transactionManager;
}
final Properties additionalProperties() {
final Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("mysql-hibernate.hbm2ddl.auto"));
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("mysql-hibernate.dialect"));
hibernateProperties.setProperty("hibernate.show_sql", env.getProperty("mysql-hibernate.show_sql") != null ? env.getProperty("mysql-hibernate.show_sql") : "false");
return hibernateProperties;
}
}
感谢任何帮助。
答案 0 :(得分:0)
我有相同的结果,在我的情况下问题是与数据库服务器有关。服务器是远程的,执行查询太慢了。当我将我的应用程序部署到同一台服务器时,它变得足够快。插入具有许多依赖项(一对多,多对一)的对象现在需要190毫秒。选择此对象需要61ms。