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