0
@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

5
  • Can you show us how you configure the 'PersistenceContext(unitName = "MainDB")'. I believe you need to define the 'javax.persistence.query.timeout' there initially and the setHint() then overrides this value. See stackoverflow.com/questions/24244621/… Commented May 31, 2021 at 9:15
  • updated my configuration. Where should i set this? Commented May 31, 2021 at 9:26
  • updated my PlatformTransactionManager, set the timeout there, but it still does not work. Commented May 31, 2021 at 9:53
  • "Depending on the persistence provider and database in use, the hint may or may not be observed." - Which DB are you using? Commented May 31, 2021 at 10:28
  • I'm using Oracle 12c. Commented May 31, 2021 at 10:40

1 Answer 1

1

i finally solved what i wanted to do by setting this property in application.yml:

spring.datasource.hikari.dataSourceProperties: oracle.jdbc.ReadTimeout=3000

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.