In the three-tier structure of Java EE, why things exist in the business layer

Source: Internet
Author: User

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

Related Article

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.