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

Source: Internet
Author: User
Tags set time socket blocking

Time-out classifications

Timeouts 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 within a set time (commit or rollback ), or a time-out 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 settings in most scenarios, the app primarily cares about transaction timeout settings unless there is 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 a global transaction Timeout for the DefaultTimeout property of TransactionManager, or in a declarative transaction (Tx:attributes) The value of the Timeout property is specified for a specific or all of the methods in the configuration, and 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 select Datasourcetransactionmanager, all SQL operations within a transaction must be performed by JdbcTemplate to enable the timeout setting to work correctly, through other ORM (such as MyBatis) The SQL operation performed will fail to apply the time-out setting. This mode requires a timeout to be configured according to the requirements of the specific ORM framework (for example, MyBatis's Defaultstatementtimeout 4) in some scenarios where there is a real mix of two 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 capture in the event that the transaction timeout setting takes effect, the framework will actively roll back the current transaction and throw Uncategorizedsqlexception after a timeout has occurred. 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 captured by the upper Transactioninterceptor and repackaged as an instance of uncategorizedsqlexception, then rollback the transaction, but because the connection is closed, the rollback exception will be thrown , so you'll see "Transactioninterceptor.completetransactionafterthrowing (Transactionaspectsupport.java:414) application exception overridden by rollback exception "Eventually, a transactionsystemexception instance is thrown to the caller, An instance of the uncategorizedsqlexception before being overwritten can be obtained by its getapplicationexception () method. Reference 1. Understanding JDBC Internals & Timeout Configuration2. Spring transaction Timeout doesn ' t work?3. The spring transaction time-out time may exist error awareness 4. Using Spring 3 with Mybatis 3 Tutorial–part 1

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

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.