DB2 uses the Hibernate Interceptor to implement dirty read (with ur) and db2hibernate

Source: Internet
Author: User

DB2 uses the Hibernate Interceptor to implement dirty read (with ur) and db2hibernate

Work needs: Recently, the development of the system to adapt to the underlying database to add support for DB2, although the use of DB2, but performance considerations, and business needs. The query does not require transaction control, that is, multiple transaction security levels of DB2. During the query, you do not need to pay attention to updates and inserts. Therefore, the query must support dirty reading. The with ur option must be added to the end of each query SQL statement.

I have been searching for it online for a long time. Many people are asking, but there is no result. Finally, find a solution on google and use the hibernate Interceptor to intercept it. The following code is used:

Import org. hibernate. emptyInterceptor; import org. slf4j. logger; import org. slf4j. loggerFactory;/*** When configuring DB2 in hibernate, in order to prevent the impact of the high transaction security level on the query, the query must be independently formulated with ur. * This type is a hibernate Interceptor Used to add the with ur option to the end of the select query to prevent locking the database during query. * @ Author superxb **/public class DB2Interceptor extends EmptyInterceptor {private static final long serialVersionUID = 1L; private static final Logger logger = LoggerFactory. getLogger (DB2Interceptor. class); @ Overridepublic String onPrepareStatement (String str) {// convert all SQL strings to lowercase String compstr = str. toLowerCase (); // All select statements, as long as they do not contain with ur. Add with urif (compstr. matches ("^ select .*")&&! Compstr. matches (". * for update. *") {if (! Compstr. matches (". * with ur. * ") {str + =" with ur "; logger. debug ("Appending \" with ur \ "to query. ") ;}} return str ;}}

After the interceptor is created, configure it in the sessionFactory of hibernate. The configuration is as follows:

<Bean id = "sessionFactory" class = "org. springframework. orm. hibernate3.annotation. annotationSessionFactoryBean "> <property name =" dataSource "> <ref local =" dataSource "/> </property> <! -- Adds the whit ur control transaction level after all SQL statements. --> <property name = "entityInterceptor"> <bean class = "interceptor. DB2Interceptor "/> </property> ............

After the above configuration. By default, as long as the SQL statement starting with select does not contain with ur, it will add with ur at the end to ensure the transaction security level. When hibernate is mapped to the DB2 database, attackers can perform dirty read operations.


Java connects to DB2 through jdbc and can use with as (select

In session. createQuery (), hql is required.

The sentence you wrote is an SQL statement.
Session. createSQLQuery (), which can execute SQL.

However, if you use hibernate, consider hql implementation. Your query is not complex.

I don't understand why you want to use subqueries.

Use of flush in hibernate

You set session. beginTransaction ();
Try to comment out this. You have used a transaction, but you have not committed it. The database has no data. It should be understandable.

Related Article

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.