为什么通过注释设置超时的 ChainedTransactionManager 不起作用?弹簧靴

问题描述

应用:

  • Spring Boot 版本 2.2.13.RELEASE
  • postgresql 版本 42.2.10
  • 石英版本 2.3.2
  • 石英作业 2.3.2 版

我已经实现了一个工作石英,它调用在@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从集群中的作业quartz中调用。服务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 秒后出现事务超时异常,但超时不起作用,为什么?

解决方法

我通过设置属性解决了我的问题,如以下文章中所述:

[https://www.theguardian.com/info/2019/dec/02/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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...