In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
We all know that in the actual development of javaee, it is divided into three-tier structure, controller,service and dao
So why do things exist in the business layer, things are operated through connection objects, links to databases using the original jdbc are also operated by connection, and connection is passed to dao?
Here are two ways to explain
The first way to pass through formal parameters
The second way through ThreadLocal
At the bottom of the ThreadLocal is a map, and the key of the map is fixed, the current thread. Value allows us to store any object.
Public class JdbcUtils {/ / member variable, which creates the connection pool for C3P0 (connection pool already exists...) private static final ComboPooledDataSource DATASOURCE = new ComboPooledDataSource (); / / binds Connection to the current thread private static ThreadLocal tl = new ThreadLocal (); / * returns C3P0 connection pool * @ return * / public static DataSource getDataSource () {return DATASOURCE } / * obtain connection, return connection * @ return * @ throws SQLException * / public static Connection getConnection () throws SQLException {Connection conn = null; / / get conn = tl.get () from tl; if (conn = = null) {/ / get connection conn = DATASOURCE.getConnection () from connection pool / / it is critical to store the connection in tl tl.set (conn);} return conn;} / * start transaction * @ throws SQLException * / public static void beginTransaction () throws SQLException {/ / call getConnection () Connection conn = getConnection (); conn.setAutoCommit (false) } / * commit transaction * @ throws SQLException * / public static void commitTransaction () throws SQLException {/ / call getConnection () Connection conn = getConnection (); conn.commit () } / * rollback transaction * @ throws SQLException * / public static void rollBackTransaction () throws SQLException {/ / call getConnection () Connection conn = getConnection (); conn.rollback () } / * return connection * @ throws SQLException * / public static void closeConn () throws SQLException {/ / call getConnection () Connection conn = getConnection (); conn.close (); tl.remove () } / * release resources * @ param stmt * @ param conn * / public static void release (Statement stmt,Connection conn) {if (stmt! = null) {try {stmt.close ();} catch (SQLException e) {e.printStackTrace () }} if (conn! = null) {try {/ / has become returned. Conn.close ();} catch (SQLException e) {e.printStackTrace () } / * release resources * @ param stmt * @ param conn * / public static void release (ResultSet rs,Statement stmt,Connection conn) {if (rs! = null) {try {rs.close ();} catch (SQLException e) {e.printStackTrace () }} if (stmt! = null) {try {stmt.close ();} catch (SQLException e) {e.printStackTrace () }} if (conn! = null) {try {/ / modified close () to destroy the connection, but now let the method return the connection. Conn.close ();} catch (SQLException e) {e.printStackTrace ();}
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.