Spring-jdbc-4.0.5.release:以编程方式设置DriverManagerDataSource连接超时

时间:2019-02-07 23:01:11

标签: java spring hibernate jdbc spring-data-jpa

我注意到SQL连接在30分钟后关闭,因此我试图将其增加到60分钟。以下是我用于Sql Server 2008的Spring Datasource配置类,正在尝试将connectiontimeout设置为ds.getConnectionProperties().put("socketTimeout", jdbcsocketTimeout);,但是在启动应用程序时遇到Error creating bean with name 'entityManagerFactory'错误。

@Configuration
@EnableJpaRepositories
public class JpaConfiguration {

    @Value("${jdbc.driverClassName}")
    String jdbcDriverClassName;
    @Value("${jdbc.url}")
    String jdbcUrl;
    @Value("${jdbc.username}")
    String jdbcUsername;
    @Value("${jdbc.password}")
    String jdbcPassword;
    @Value("${jdbc.socketTimeout}")
    int jdbcsocketTimeout;

    @Bean
    public javax.sql.DataSource remoteDataSource() {
        DriverManagerDataSource ds = new DriverManagerDataSource(jdbcUrl, jdbcUsername, jdbcPassword);
        ds.setDriverClassName(jdbcDriverClassName);
        System.out.println("jdbcsocketTimeout "+jdbcsocketTimeout);
        //ds.getConnectionProperties().put("socketTimeout", jdbcsocketTimeout);
        return ds;
    }

    @Bean
    public Map<String, Object> jpaProperties() {
        Map<String, Object> props = new HashMap<String, Object>();      
        props.put("hibernate.dialect", SQLServer2008Dialect.class.getName());       
        return props;
    }

    @Bean
    public JpaVendorAdapter jpaVendorAdapter() {
        HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();      
        hibernateJpaVendorAdapter.setGenerateDdl(false);
        hibernateJpaVendorAdapter.setDatabase(Database.SQL_SERVER);     
        return hibernateJpaVendorAdapter;
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        return new JpaTransactionManager(localContainerEntityManagerFactoryBean().getObject());
    }

    @Bean(name = "entityManagerFactory")
    public LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean() {
        LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
        lef.setDataSource(this.remoteDataSource());
        lef.setPackagesToScan(getClass().getPackage().getName());
        lef.setJpaPropertyMap(this.jpaProperties());
        lef.setJpaVendorAdapter(this.jpaVendorAdapter());
        return lef;
    }
}

有人可以帮我在上述课程中设置连接超时吗?

0 个答案:

没有答案