为什么通过注解设置超时的chainedtransactionmanager不起作用?Spring Boot

tpxzln5u  于 2021-07-23  发布在  Java
关注(0)|答案(1)|浏览(445)

应用程序:
spring boot版本2.2.13.release
postgresql版本42.2.10
石英版本2.3.2
石英作业版本2.3.2
我已经实现了一个job quartz,它调用@servicebean中实现的方法。在这个方法中,有两个数据库(oracle和postgresql)的交互。每个数据库都使用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是从集群中的job 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() );

在amazonrds-postgresql上执行failoverdb操作(与db的连接丢失)之后,我会在120秒之后出现事务超时异常,但是超时不起作用,为什么?

k3fezbri

k3fezbri1#

我解决了以下文章中所述的设置属性的问题:
[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

相关问题