Reputation: 11
@Repository
public class MyRepository {
@PersistenceContext(unitName = "MainDB")
private EntityManager em;
public List<Info> getData(String p1, String p2) {
StoredProcedureQuery sp = em.createNamedStoredProcedureQuery("myProc")
.setParameter("p1", p1)
.setParameter("p2", p2)
.setHint("javax.persistence.query.timeout", 3000);
return (List<Info>) sp.getResultList();
}
}
DB Configuration:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactory", basePackages = {
"com.mypackage.repository" })
public class MainDBConfig {
@Primary
@Bean(name = "dataSource")
@ConfigurationProperties
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder,
@Qualifier("dataSource") DataSource dataSource) {
HashMap<String, Object> properties = new HashMap<>();
// properties.put("hibernate.hbm2ddl.auto", "update");
properties.put("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
properties.put("hibernate.proc.param_null_passing", true);
return builder.dataSource(dataSource).properties(properties)
.packages("com.mypackage.entity").persistenceUnit("MainDB").build();
}
@Primary
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager(
@Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
changed my transaction manager to below but still does not work:
@Primary
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager(
@Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory) {
JpaTransactionManager tm = new JpaTransactionManager(entityManagerFactory);
tm.setDefaultTimeout(3);
return tm;
}
The stored procedure is still running after 3 seconds. My goal is to throw an exception if it exceeds 3 seconds and get the connection released back to the pool.
Spring Boot Version: 2.2.1.RELEASE, Hibernate version: 5.4.8
Upvotes: 0
Views: 1208
Reputation: 11
i finally solved what i wanted to do by setting this property in application.yml:
spring.datasource.hikari.dataSourceProperties: oracle.jdbc.ReadTimeout=3000
Upvotes: 1