为什么通过注释设置超时的 ChainedTransactionManager 不起作用? Spring开机

Why ChainedTransactionManager with setting timeout via annotation not working? Spring boot

申请:

我已经实现了一个作业 quartz,它调用在 @Service bean 中实现的方法。在这个方法中有与两个数据库(Oracle 和 PostgreSql)的交互。每个 Db 都使用 Hikari 连接池初始化

配置 bean 中的 Postgresql 数据库:

        @Bean(value="sreDataSource")
        public DataSource dataSource() {
        
            HikariConfig hikariConfig = new HikariConfig();
            hikariConfig.setDriverClassName(driverClassName);
            hikariConfig.setJdbcUrl(datasourceUrl); 
            hikariConfig.setUsername(datasourceUsername);
            hikariConfig.setPassword(datasourcePassword);
            hikariConfig.setConnectionInitSql("set time zone 'UTC'");
            hikariConfig.setMaximumPoolSize(maxPoolSize);
            hikariConfig.setConnectionTestQuery("SELECT 1");
            hikariConfig.setPoolName("springHikariCP");
        
            return new HikariDataSource(hikariConfig);
            
        }
    /**
         * @return {@link JdbcTemplate}
         */
        @Bean(value="sreJdbcTemplate")
        public JdbcTemplate jdbcTemplate() {
            
            JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource());
            jdbcTemplate.setFetchSize(Integer.parseInt(fetchSize));
            jdbcTemplate.setQueryTimeout(60);
            
            return jdbcTemplate;
        }
    
        
        /**
         * Create the namedJdbcTemplate bean for the SRE database
         * @return {@link NamedParameterJdbcTemplate}
         */
        @Bean(value="sreNamedParameterJdbcTemplate")
        public NamedParameterJdbcTemplate namedParameterJdbcTemplate() {
            
            return new NamedParameterJdbcTemplate(jdbcTemplate());
        }
        
        
        /**
         * @param dataSource
         * @return {@link PlatformTransactionManager}
         */
        @Bean(value="sreTransactionManager")
        public PlatformTransactionManager transactionManager(@Qualifier("sreDataSource") DataSource dataSource) {
            
            return new DataSourceTransactionManager(dataSource);
        }
/**
     * @param sreTransactionManager
     * @param assetTransactionManager
     * @return {@link ChainedTransactionManager}
     */
    @Bean(value="chainedTransactionManager")
    public ChainedTransactionManager transactionManager(@Qualifier("sreTransactionManager") PlatformTransactionManager sreTransactionManager,
                                                    @Qualifier("assetTransactionManager") PlatformTransactionManager assetTransactionManager) {
         return new ChainedTransactionManager(sreTransactionManager, assetTransactionManager);
    }

配置 bean 中的 Oracle DB:

/**
     * Datasource
     * @return Datasource
     */
    @Bean(value="assetDataSource")
    public DataSource dataSource() {
        
        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setDriverClassName(driverClassName);
        hikariConfig.setJdbcUrl(datasourceUrl); 
        hikariConfig.setUsername(datasourceUsername);
        hikariConfig.setPassword(datasourcePassword);
        hikariConfig.addDataSourceProperty("oracle.jdbc.timezoneAsRegion", "false");
        hikariConfig.setMaximumPoolSize(maxPoolSize);
        hikariConfig.setConnectionTestQuery("SELECT 1 from dual");
        hikariConfig.setPoolName("springHikariAssetCp");
        
        
        return new HikariDataSource(hikariConfig);
    }
    
    /**
     * JDBCTemplate
     * @return JDBCTemplate
     */
    @Bean(value="assetJdbcTemplate")
    public JdbcTemplate jdbcTemplate() {
        
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource());
        jdbcTemplate.setFetchSize(Integer.parseInt(fetchSize));
        jdbcTemplate.setQueryTimeout(60);
        
        return jdbcTemplate;
    }
    
    /**
     * Create the namedJdbcTemplate bean for the RMD database
     * @return NamedParameterJdbcTemplate
     */
    @Bean(value="assetNamedParameterJdbcTemplate")
    public NamedParameterJdbcTemplate namedParameterJdbcTemplate() {
        
        return new NamedParameterJdbcTemplate(jdbcTemplate());
    }
    
    /**
     * Create transaction manager
     * @param dataSource Datasource
     * @return transaction manager
     */
    @Bean(value="assetTransactionManager")
    public PlatformTransactionManager transactionManager(@Qualifier("assetDataSource") DataSource dataSource) {
        
        return new DataSourceTransactionManager(dataSource);
    }

服务 bean 从工作石英中调用 cluster.The 服务 bean 定义如下:

/**
 * @author sgalgani
 *
 */
@Service("processSreService")
@Transactional("chainedTransactionManager")
public class ProcessSreServiceImpl implements ProcessSreService {

    private static final Logger LOGGER = LogManager.getLogger(ProcessSreServiceImpl.class);

    @Autowired
    @Qualifier("historianService")
    private HistorianService historianService;

    @Autowired
    @Qualifier("sreEvolutionService")
    private SreEvolutionService sreEvolutionService;

    @Autowired
    @Qualifier("assetService")
    private AssetService assetService;

    @Autowired
    @Qualifier("configService")
    private ConfigService configService;

    @Autowired
    @Qualifier("engineSupportService")
    private EngineSupportService engineSupportService;

    @Autowired
    @Qualifier("tagAvailabilityService")
    private TagAvailabilityService tagAvailabilityService;

    @Autowired
    @Qualifier("engineTransformService")
    private EngineTransformService engineTransformService;

    @Autowired
    @Qualifier("quarantineService")
    private QuarantineService quarantineService;

    @Autowired
    @Qualifier("simpleRuleEventService")
    private SimpleRuleEventService simpleRuleEventService;

    @Value("${save.oracle.temporary.results}")
    private boolean saveOracleTemporaryResults;

    @Value("${enable.save.local.db.alerts}")
    private boolean enableSaveLocalDbAlerts;
    
    @Value("${simple.rule.event.service.enable}")
    private boolean simpleRuleEventServiceEnable;

    @SuppressWarnings("javadoc")
    @Override
    @Transactional(value = "chainedTransactionManager", rollbackFor = Exception.class, timeout = 120)
    public void processSre(Sre sre, List<TagLastTimestamp> tagsLastTimestamp, Metric metrics) throws Exception {

        LOGGER.info("[processSre] for JobId : {} and lineup: {} ",sre.getId(), sre.getLineupId() );

在 Amazon RDS postgresql 上执行故障转移数据库操作后(失去与数据库的连接),我会在 120 秒后执行事务超时异常,但超时不起作用,为什么?

我解决了设置 属性 的问题,如下文所述:

faster-postgresql-connection-recovery

我的代码添加配置HikariCP:

hikariConfig.addDataSourceProperty("socketTimeout", "120");

socketTimeout 到 120 秒,所以在任何情况下 属性 都会在超时后抛出以下异常:

Caused by: java.net.SocketTimeoutException: Read timed out at java.net.SocketInputStream.socketRead0(Native Method) ~[?:?]    
at java.net.SocketInputStream.socketRead(SocketInputStream.java:115) ~[?:?]
at java.net.SocketInputStream.read(SocketInputStream.java:168) ~[?:?]   
at java.net.SocketInputStream.read(SocketInputStream.java:140) ~[?:?]  
at sun.security.ssl.SSLSocketInputRecord.read(SSLSocketInputRecord.java:448) ~[?:?]   
at sun.security.ssl.SSLSocketInputRecord.bytesInCompletePacket(SSLSocketInputRecord.java:68) ~[?:?]
at sun.security.ssl.SSLSocketImpl.readApplicationRecord(SSLSocketImpl.java:1103) ~[?:?]
at sun.security.ssl.SSLSocketImpl$AppInputStream.read(SSLSocketImpl.java:823) ~[?:?]
at org.postgresql.core.VisibleBufferedInputStream.readMore(VisibleBufferedInputStream.java:161) ~[postgresql-42.2.10.jar!/:42.2.10]
at org.postgresql.core.VisibleBufferedInputStream.ensureBytes(VisibleBufferedInputStream.java:128) ~[postgresql-42.2.10.jar!/:42.2.10]
at org.postgresql.core.VisibleBufferedInputStream.ensureBytes(VisibleBufferedInputStream.java:113) ~[postgresql-42.2.10.jar!/:42.2.10]
at org.postgresql.core.VisibleBufferedInputStream.read(VisibleBufferedInputStream.java:73) ~[postgresql-42.2.10.jar!/:42.2.10]
at org.postgresql.core.PGStream.receiveChar(PGStream.java:370) ~[postgresql-42.2.10.jar!/:42.2.10]
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2015) ~[postgresql-42.2.10.jar!/:42.2.10]
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:311) ~[postgresql-42.2.10.jar!/:42.2.10]... 17 more