DB2 using hibernate interceptors for dirty reads (with ur)

Source: Internet
Author: User



Work needs, recently to allow the development of the system to adapt the database to increase the support of DB2, although the use of DB2, but in terms of performance considerations, and business needs. The query does not require transaction control, that is, a variety of transaction security levels for DB2, and there is no need to focus on updates and insertions when querying. Therefore, a query is required to support dirty reads. The WITH UR option is added after the SQL statement for each query.



On the internet for a long time, a lot of people are asking, but no results. Finally, find a solution in Google and use the Hibernate interceptor to intercept. Here's the code:




import org.hibernate.EmptyInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
* *
*When hibernate configures DB2, in order to prevent the high transaction security level from affecting the query, the query needs to formulate with ur separately.
*This class is a hibernate interceptor, which is used to add the with ur option to the end of the query of select to prevent the database from being locked during the query.
* @author superxb
*
* /
public class DB2Interceptor extends EmptyInterceptor {
private static final long serialVersionUID = 1L;
private static final Logger logger = LoggerFactory
.getLogger(DB2Interceptor.class);
@Override
public String onPrepareStatement(String str) {
//All SQL strings are converted to lowercase
String compstr = str.toLowerCase();
//All select statements, as long as they do not contain with ur. Add with ur at the end
if (compstr.matches("^select.*") && !compstr.matches(".*for update.*")) {
if (!compstr.matches(".*with ur.*")) {
str += " with ur ";
logger.debug("Appending \"WITH UR\" to query.");
}
}
Return str;
}
}





Once the interceptor is created, configure the sessionfactory in Hibernate. The configuration reference is as follows:





<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property>
<! -- specifically for the blocker added to DB2, add the when ur control transaction level after all SQL -- >
<property name="entityInterceptor">
<bean class="interceptor.DB2Interceptor" />
</property>
...
... 

Once configured as above. By default, as long as the SQL statement at the beginning of the select, which does not contain with UR, will add with ur at the end to ensure the transaction security level, when implementing Hibernate mapping DB2 database, can be dirty read operation.





DB2 using hibernate interceptors for dirty reads (with ur)


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.