首页 > 解决方案 > Spring-jdbc-4.0.5.release:以编程方式设置 DriverManagerDataSource 连接超时

问题描述

我注意到 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;
    }
}

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

标签: javaspringhibernatejdbcspring-data-jpa

解决方案


推荐阅读