How to control timeouts when data access is based on JDBC in spring

Source: Internet
Author: User
Tags set time socket blocking

time-out classificationstimeouts can be categorized by scope as follows:Transaction Timeout > Statement timeout > JDBC Driver Socket timeoutTransaction timeout refers to a set of SQL operations that should be completed (committed or rolled back) within a set time, otherwise a timeout will be thrown. Its value should be greater than N (number of statements) * Statement TimeoutStatement timeout refers to the maximum allowable time to complete a single SQL statement execution. Its value should be less than the value of the JDBC Driver Socket timeout, since the latter will be checked first. The lowest-level JDBC Driver socket timeout refers to a time-out when connecting through a Socket or a blocking timeout when reading or writing, if not set to use the OS layer's socket timeout value.
Timeout Settingsin most scenarios, the app primarily cares about transaction timeout settings, unless there's always a single statement in transaction. the specific settings for timeouts depend on the data access framework used or the JDBC Driver. If you use spring as the underlying framework, you can set the global TransactionManager by setting the DefaultTimeout property for theTransaction Timeout, or specify the value of the Timeout property for a specific or all method in the configuration of a declarative transaction (Tx:attributes). It can also be set by the timeout parameter in the annotated transaction @transactional tag. Note:1. " Spring Transaction Timeout = When the transaction started to the last statement creation time + The last statement execution time-out time (that is, its querytimeout). "(3) 2. If you select Jpatransactionmanager as the transaction manager, you need the spring version on the 3.0.0 to make the timeout setting work correctly;3. If you selectDatasourcetransactionmanager, all SQL operations within a transaction must be performed through JdbcTemplate tomaking the timeout setting work correctly, SQL operations performed through other ORM (such as MyBatis) will not be able to apply time-out settings. This mode requires a timeout to be configured according to the requirements of the specific ORM framework (e.g. MyBatis's Defaultstatementtimeout 4)
in some scenarios, there is a real case of mixing two kinds of data access frameworks (let's not discuss whether this is justified). For example, mixing jdbctemplate and MyBatis in the same transaction, in which timeout set for @transactional or tx:attributes will not take effect for operations that are not jdbctemplate parts. Therefore, the timeout for this transaction is not actually controlled. At this point, there is a "workaround" way to enable the two data access frameworks to "share" the timeout setting, which is to set the JDBC Driver socket blocking timeout. If you are using an Oracle database and the data source uses DBCP's Basicdatasource, you can set this value in its ConnectionProperties property, for example:
<bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource" destroy-method= "Close" >        <!-- Set the blocking timeout for millisecond units--        <property name= "connectionproperties" value= "oracle.net.read_timeout=1000"/>          <!--other property settings ...--></bean>

If multiple properties are to be set at the same time, they are separated by semicolons in value. other database settings are described in Data 3.

Timeout Capturein the event that the transaction timeout setting is in effect, the framework will proactively roll back the current transaction and throwuncategorizedsqlexception. This is a subclass of RuntimeException, with its getsql () or GetMessage () method to know the SQL statement that raised the timeout. when a timeout is blocked using the JDBC Driver socket, the current connection is closed by Driver, and the operation being performed in the transaction, whether through JdbcTemplate or another ORM framework, is raised SqlException ( Exception information is: Closed connection), this exception will be the upperTransactioninterceptor captured and repackaged into athe Uncategorizedsqlexception instance, and then rolls back the transaction, but because the connection is closed, a rollback exception is thrown, so you see"transactioninterceptor.completetransactionafterthrowing(Transactionaspectsu pport.java:414)application Exception overridden by rollback exception"Eventually, the caller is thrown aThe transactionsystemexception instance, through its getapplicationexception () method, can obtain thean instance of uncategorizedsqlexception.
References1. Understanding JDBC Internals & Timeout Configuration
2. Spring transaction Timeout doesn ' t work?
3. Possible error awareness of spring transaction timeout time
4. Using Spring 3 with Mybatis 3 Tutorial–part 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.