ThreadLocal solving concurrency problems for multithreaded threads + transactional processing

Source: Internet
Author: User

1 Importjava.sql.Connection;2 Importjava.sql.SQLException;3 4  Public classTranmanager {5     Private StaticThreadlocal<connection> tl=6                     NewThreadlocal<connection>(){7         /**8 * Internal Method9 * Returns the initial value of the local variable of the thread backTen * Initializes the thread, which is called every time get () or set (object).  One          */ A         protectedConnection InitialValue () { -             Try { -                 returndaoutils.getconn (); the}Catch(Exception e) { - e.printstacktrace (); -                 return NULL; -             } +         } -     }; +     PrivateTranmanager () {} A      Public StaticConnection Getconn () { at         returntl.get (); -     } -     /** -      * @returnTurn on manual commit transactions, - * @false: Handing SQL commands to application management -      */ in      Public Static voidStarttran () { -         Try { toTl.get (). Setautocommit (false); +}Catch(SQLException e) { - e.printstacktrace (); the         } *     } $     /**Panax Notoginseng      * @returnrolling back -      */ the      Public Static voidRollbacktran () { +         Try { A tl.get (). rollback (); the}Catch(SQLException e) { + e.printstacktrace (); -         } $     } $     /** -      * @returnExecution -      */ the      Public Static voidCommittran () { -         Try {Wuyi Tl.get (). commit (); the}Catch(SQLException e) { - e.printstacktrace (); Wu         } -     } About     /** $      * @returnClose -      */ -      Public Static voidrelease () { -         Try { A Tl.get (). Close (); +Tl.remove ();//Map{tl:conn} the}Catch(SQLException e) { - e.printstacktrace (); $         } the     } the     /*Public void Rollbacktran (SavePoint sp) { the try { the Conn.rollback (SP); - conn.commit (); in } catch (SQLException e) { the e.printstacktrace (); the         } About     }*/ the}

ThreadLocal solving concurrency problems for multithreaded threads + transactional processing

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.