Mix spring jdbctemplate and hiberante

Source: Internet
Author: User

Jdbctemplate was used in the project at the beginning. To improve the development efficiency, Hibernate was used. However, it was found that hibernate was not very efficient during batch operations. So now we use jdbctemplate and Hibernate to mix, jdbctemplate and hibernate can be mixed, there are several considerations.

1. If the jdbctemplate part only involves queries, you can use the hibernate application cache, that is, the second-level cache.

2. if the jdbctemplate is used, the database update operation is involved, that is, adding, deleting, and modifying. the second-level cache of hibernate cannot be enabled. If the system requires caching, I think I can implement caching on the logic layer. there are still many Java cache solutions.

3. Mix jdbctemplate and hibernate in the system using spring as the container. Use hibernatetransactionmana in transaction management.The premise is that jdbctemplate and hibernate share a datasource.

There is an original saying in the class description of hibernatetransactionmanager

This Transaction Manager is appropriate for applications that use a single hibernate sessionfactory for transactional data access, but it also supports direct datasource access within a transaction (I. e. plain JDBC code working with the same datasource ). this allows for mixing services which access Hibernate and services which use plain JDBC (without being aware of hibernate )! Application code needs to stick to the same simple connection lookup pattern as with datasourcetransactionmanAger (I. e. datasourceutils. getconnection (javax. SQL. datasource) or going through a transactionawaredatasourCeproxy ).

SlaveCodeTransactions can also be mixed. transactions can be managed in a unified manner. HibernatetransactionmanaThere is such a piece of code in ger's dobegin method.

1 // Add the hibernate transaction to the session holder
2 Txobject. getsessionholder (). settransaction (session. begintransaction ());
3 // Register transaction timeout
4 If (Definition. gettimeout () ! = Transactiondefinition. timeout_default)
5
6 {
7 Txobject. getsessionholder (). settimeoutinseconds (definition. gettimeout ());
8 }
9 // Bind the session holder to the thread
10 If (Txobject. isnewsessionholder ())
11 {
12 Transactionsynchronizationmanager. bindresource ( This . Sessionfactory, txobject. getsessionholder ());
13 }
14 // Register the hibernate session's JDBC connection for the datasource, if set
15 If ( This . Datasource ! =   Null )
16
17 {
18 Connectionholder conholder =   New Connectionholder (session. Connection ());
19 If (Definition. gettimeout () ! = Transactiondefinition. timeout_default)
20 {
21 Conholder. settimeoutinseconds (definition. gettimeout ());
22 }
23 Transactionsynchronizationmanager. bindresource ( This . Datasource, conholder );
24 }
25
26

Note that this AnnotationRegister the hibernate session's JDBC connection for the datasource, if set

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.