问题
please tell me. How can I configure the "Hikari connection pooling + Hibernate 4.3.8 + Spring Data JPA configuration"? Here is my configuration, but for some reason I am sure that this configuration is not correct. Because the the console does not appear anything like the "connetction pooling". On the Internet I can not find the tutorial for the beginner. Thanks.
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="dataSource" p:packagesToScan="com.history"
p:jpaVendorAdapter-ref="jpaVendorAdapter">
<property name="jpaProperties" ref="hibernateProperties" />
</bean>
<bean id="jpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
<property name="database" value="ORACLE" />
<property name="databasePlatform" value="org.hibernate.dialect.OracleDialect" />
</bean>
<util:map id="hibernateProperties">
<entry key="hibernate.connection.driver_class"
value="org.hibernate.hikaricp.internal.HikariCPConnectionProvider" />
<entry key="connection.url" value="${dataSource.url}" />
<entry key="connection.driver_class" value="${dataSource.driverClassName}" />
<entry key="connection.username" value="${dataSource.username}" />
<entry key="connection.password" value="${dataSource.password}" />
<entry key="hibernate.jdbc.batch_size" value="100" />
<entry key="hibernate.order_inserts" value="true" />
<entry key="hibernate.order_updates" value="true" />
</util:map>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${dataSource.driverClassName}" />
<property name="url" value="${dataSource.url}" />
<property name="username" value="${dataSource.username}" />
<property name="password" value="${dataSource.password}" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory">
</bean>
回答1:
If you want to use Hikari, you'll need to declare it, so your dataSource config should be
<bean id="dataSource"
class="com.zaxxer.hikari.HikariDataSource">
<property name="driverClassName" value="${dataSource.driverClassName}" />
<property name="jdbcUrl" value="${dataSource.url}" />
<property name="username" value="${dataSource.username}" />
<property name="password" value="${dataSource.password}" />
</bean>
回答2:
Your datasource config should be like this:
<beans:bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<beans:property name="dataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlDataSource"/>
<beans:property name="maximumPoolSize" value="5" />
<beans:property name="maxLifetime" value="30000" />
<beans:property name="idleTimeout" value="30000" />
<beans:property name="dataSourceProperties">
<beans:props>
<beans:prop key="url">jdbc:mysql://localhost:3306/exampledb</beans:prop>
<beans:prop key="user">root</beans:prop>
<beans:prop key="password"></beans:prop>
<beans:prop key="prepStmtCacheSize">250</beans:prop>
<beans:prop key="prepStmtCacheSqlLimit">2048</beans:prop>
<beans:prop key="cachePrepStmts">true</beans:prop>
<beans:prop key="useServerPrepStmts">true</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
Complete example with sample project to download can be found in link : http://frameworkonly.com/hikaricp-connection-pooling-in-spring-hibernate-jpa/
回答3:
We can also use it like below in customized JPAConfiguration class where you are establishing connection and creating entity Manager Factory object.
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
@Bean
public DataSource dataSource() {
// In classpath from spring-boot-starter-web
final Properties props = new Properties();
props.put("driverClassName", "com.mysql.jdbc.Driver");
props.put("jdbcUrl", "jdbc:mysql://localhost:3306/master?createDatabaseIfNotExist=true");
props.put("username", "root");
props.put("password", "mysql");
HikariConfig hc = new HikariConfig(props);
HikariDataSource ds = new HikariDataSource(hc);
return ds;
}
来源:https://stackoverflow.com/questions/29567127/hikari-connection-pooling-hibernate-4-3-8-spring-data-jpa-configuration