Skip to content

Instantly share code, notes, and snippets.

@brmeyer
Created April 23, 2014 01:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brmeyer/bbb8688658dd71752738 to your computer and use it in GitHub Desktop.
Save brmeyer/bbb8688658dd71752738 to your computer and use it in GitHub Desktop.
@Configuration
@EnableTransactionManagement
...
public class AppConfig {
...
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setHibernateProperties(hibernateProperties());
...
return sessionFactory;
}
@Bean
public HibernateTransactionManager transactionManager() {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory().getObject());
return txManager;
}
private DataSource dataSource() {
...
final HikariDataSource ds = new HikariDataSource();
ds.setMaximumPoolSize(100);
ds.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource");
ds.addDataSourceProperty("url", url);
ds.addDataSourceProperty("user", username);
ds.addDataSourceProperty("password", password);
ds.addDataSourceProperty("cachePrepStmts", true);
ds.addDataSourceProperty("prepStmtCacheSize", 250);
ds.addDataSourceProperty("prepStmtCacheSqlLimit", 2048);
ds.addDataSourceProperty("useServerPrepStmts", true);
return ds;
}
private Properties hibernateProperties() {
final Properties properties = new Properties();
... (Dialect, 2nd level entity cache, query cache, etc.)
return properties;
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment