We all know that in Java EE Practical development, there are 3 layers of structure to develop, Controller,service and DAO
So why do things exist in the business layer, things are manipulated by connection objects, the link to the database using the original JDBC link is also connection operation, connection is how to pass to DAO?
Here are two ways of explaining
The first way to pass formal parameters
The second way through threadlocal
The bottom of the threadlocal is a map, the map key is fixed, the current thread. Value allows us to deposit any object
public class Jdbcutils {///member variable, created C3P0 connection pool (connection pool already exists ... private static final Combopooleddatasource DATASOURCE = new Combopooleddatasource (); Bind the Connection to the current thread in private static threadlocal<connection> TL = new threadlocal<connection> (); /** * Returns the C3P0 connection pool * @return */public static DataSource Getdatasource () {return DataSource; /** * Get connection, return connection * @return * @throws SQLException */public static Connection getconnection () thro WS sqlexception{Connection conn = null; Get conn = Tl.get () from TL; if (conn = = null) {//Get connection from connection Pool conn = Datasource.getconnection (); Very critical, the connection is deposited into the TL Tl.set (conn); } return conn; }/** * Open 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 con N) {if (stmt! = null) {try {stmt.close (); } catch (SQLException e) {e.printstacktrace (); }} if (conn! = null) {try { has become the return of ... conn.close (); } catch (SQLException e) {e.printstacktrace (); }}}/** * Free resources * @param stmt * @param conn * * public static void release (ResultSet rs,st Atement 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 {//has modified the close (), it was destroyed, and now the method becomes the return connection. Conn.close (); } catch (SQLException e) {e.printstacktrace (); } } }}
Why things exist in the business layer in the three-tier structure of Java EE