我正在尝试使用JPA事务管理器在我的Spring + Hibernate应用程序(Java配置)中配置C3PO,但无法使其工作。以下是我的HibernateConfig类。请帮忙!
PS:最初,我已将HibernateConfig类配置为使用HibernateTransactionManager并且我已成功配置C3PO,但当我切换到使用JPA事务管理器和EntityManagerFactory时,设置C3PO属性不起作用什么都没有。我尝试使用nativ C3PO键以及键的Hibernate版本设置C3PO属性,但无济于事。package com.company.core.configuration;
import java.util.Properties;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableTransactionManagement
@ComponentScan({"com.company.core.data"})
public class EntityManagerFactoryConfiguration {
public EntityManagerFactoryConfiguration() {
super();
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan(new String[]{"com.company.core.data.pojos"});
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect");
hibernateProperties.setProperty("hibernate.show_sql", "false");
// hibernateProperties.setProperty("hibernate.connection.provider_class", "org.hibernate.connection.C3P0ConnectionProvider");
// hibernateProperties.setProperty("hibernate.c3p0.max_size", "100");
// hibernateProperties.setProperty("hibernate.c3p0.min_size", "0");
// hibernateProperties.setProperty("hibernate.c3p0.acquire_increment", "1");
// hibernateProperties.setProperty("hibernate.c3p0.idle_test_period", "200");
// hibernateProperties.setProperty("hibernate.c3p0.max_statements", "0");
// hibernateProperties.setProperty("hibernate.c3p0.timeout", "300");
em.setJpaProperties(hibernateProperties);
return em;
}
@Bean
public DataSource dataSource() {
final org.springframework.jdbc.datasource.DriverManagerDataSource dataSource = new org.springframework.jdbc.datasource.DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://127.0.0.1/databasename?autoReconnect=true");
dataSource.setUsername("user");
dataSource.setPassword("password");
return dataSource;
}
@Bean
public DataSource c3p0dataSource() {
final com.mchange.v2.c3p0.DriverManagerDataSource dataSource = new com.mchange.v2.c3p0.DriverManagerDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1/databasename?autoReconnect=true");
dataSource.setUser("user");
dataSource.setPassword("password");
return dataSource;
}
@Bean
public PlatformTransactionManager transactionManager(final EntityManagerFactory emf) {
final JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}