gpt4 book ai didi

mysql - 实体管理器超时

转载 作者:行者123 更新时间:2023-11-30 22:26:04 25 4
gpt4 key购买 nike

我正在使用 Spring Data,但对于自定义查询,我想开始使用 EntityManager。问题是除了使用 EntityManager 的查询之外,所有使用 Spring Data 的查询都工作正常。 EntityManager 查询一直出现超时错误。

@Bean
public BasicDataSource dataSource() {
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl(MessageFormat.format(Constants.DATABASE_CONNECTION_URL, host, port, schema));
ds.setUsername(username);
ds.setPassword(password);
ds.setTestWhileIdle(true);
ds.setValidationQuery(validationQuery);
ds.setTimeBetweenEvictionRunsMillis(validationTestInterval);
return ds;
}

@Bean
public EntityManager entityManager() {
return entityManagerFactory(dataSource()).createEntityManager();
}

@Bean
public EntityManagerFactory entityManagerFactory(BasicDataSource dataSource) {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(true);
vendorAdapter.setShowSql(false);
vendorAdapter.setDatabasePlatform("org.hibernate.dialect.MySQL5InnoDBDialect");
vendorAdapter.setDatabase(Database.MYSQL);

LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan(
"com.xxx"
);

Properties properties = new Properties();
properties.setProperty("hibernate.connection.provider_class", "org.hibernate.connection.C3P0ConnectionProvider");
properties.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
properties.setProperty("hibernate.connection.url", MessageFormat.format(Constants.DATABASE_CONNECTION_URL, host, port, schema));
properties.setProperty("hibernate.connection.username", username);
properties.setProperty("hibernate.connection.password", password);
properties.setProperty("hibernate.c3p0.acquire_increment", "5");
properties.setProperty("hibernate.c3p0.idle_test_period", "300");
properties.setProperty("hibernate.c3p0.min_size", "5");
properties.setProperty("hibernate.c3p0.timeout", "1000");

factory.setJpaProperties(properties);
factory.afterPropertiesSet();

return factory.getObject();
}

@Bean
public JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
JpaTransactionManager txManager = new JpaTransactionManager();
JpaDialect jpaDialect = new HibernateJpaDialect();
txManager.setEntityManagerFactory(entityManagerFactory);
txManager.setJpaDialect(jpaDialect);
txManager.setDefaultTimeout(60000);
txManager.setRollbackOnCommitFailure(true);
return txManager;
}



@Component
public class ReportGenerator {

@Autowired
private EntityManager entityManager;

private List<MyObject> doSomething() {
StringBuilder sql = new StringBuilder();
// build query
List<MyObject> list = entityManager.createNativeQuery(sql.toString(), MyObject.class).getResultList();
entityManager.clear();
return list;
}

有什么建议吗?

更新 1:

validationQuery = SELECT 1;
validationTestInterval = 30000

最佳答案

@PersistenceContext

@PersistenceContext
private EntityManager entityManager;

更新:

在我看来:这个简短的回答总比没有好,它可以节省我很多时间

link 1

That's fine, but how do I get an EnityManager? This might actually be the coolest part. Basically you’d just define a DAO exactly the way you would if you weren’t using Spring by adding the @PersistenceContext JPA annotation.

link 2 (rus)

  1. В бинах, где необходим менеджер сущностей пометить соответствующее поле аннотацией @PersistenceContext

link 3

@PersistenceContext injects a container-managed instance.

关于mysql - 实体管理器超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35158116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com