Spring Data JPA: very slow insert and select queries

Viewed 4266

I have a pretty simple spring-data-jpa + Hibernate application, which stores customers data in MySql Database (the full source code is posted here).

The problem is that it runs insert and select queries very slowly, comparing to what I have through mysql CLI:

  • insert one row takes ~3600ms,
  • select all (just 2 rows) takes ~1700ms
  • both queries take in mysql CLI about 0.12s.

There is a similar problem discussed here, however in my case the measurements are way worse (even though I don't insert batch, it's just one simple row in DB). Is there any way to improve performance in Spring JPA/Hibernate?

Another question, is there any way to reduce size of spring-data-jpa and hibernate-entitymanager? I was able to exclude byte-buddy and jandex dependencies without harm to the program, but that's only couple Mbs (the shaded jar size is down from 19.6Mb to 16.6Mb)?

UPDATE As per request here is the code (the all sources are 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
}

Here is Spring application and also saving (insert) customer:

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());
...
}

And configuration:

@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;
  }

}

Appreciate any help.

0 Answers
Related