DAO programming

xiaoxiao2021-03-06  106

One. DAO Basic Principle DAO Realization Basic Components: 1. A DAO Factory Class. 2. A DAO Interface. 3. A specific class that implements the DAO interface (contains logic to access data from the data source). 4. A data transfer Object (value object). II. Transaction Demarction DAOS Are Transactional Objects. Transaction is an important concept in DAO mode, all of DAO's operation, such as increase, delete, and update operations are based on the basis of the transaction. Therefore, the transaction boundary is a very important concept. There are two models of the transaction line: Programmatic and DeclarativeProgrammatic: Programmer is responsible for the logic of the transaction, the application controls the transaction through the API. Declarative: Programmer's deployment descriptor using EJB To declare the transaction attribute, the operating environment (EJB container) uses this property dynamic management transaction. You must consider the problem when designing: 1. How to start. 2. How to end. 3. That object is responsible for starting business. 4. That object Responsible for ending business. 5. Dao is responsible for starting and ending business. 6. Whether the application needs to across multiple DAO access data. 7. A transaction involves one or multiple DAOs. 8. Whether a DAO wants to call another DAO method Two major transactional line strategies in .dao: 1. DAO is responsible for dividing the transaction boundaries, embedding transaction code in the DAO class. 2. Call the object of the DAO method is responsible for dividing the transaction boundary, the transaction code is outside the DAO. Especially suitable for one The transaction needs to access multiple DAOs. For example, the following code: tx.begin (); // start the transactions (profile); Dao.UpdateWarehouseStatus (ID1, status1); DAO.UPDATEWAREHOUSTATUS (ID2, status2); TX .Commit (); // end The TransactionDao transaction strategy is mainly through JDBC API or JTA, JDBC implementation is simpler than JTA, but no JTA flexible JDBC delimited transaction: JDBC controls transactions through the Connection object, the Connection object is available Two modes are controlled: Auto Submit and Manual Submit. Public Void SetAutocommit (Boolean) P Ublic Boolean getAutoCommit () public void.com () public voidback () Public voidback () can perform multiple SQL statements in a separate transaction, but cannot span multiple databases, the scope of transactions are restricted in one database Medium. *; Import javax.sql. *; // ... DataSource DS = ObtainDataSource (); connection conn = ds.getConnection (); conn.setautocommit (false); // ... PSTMT = conn.preparestatement ("Update Movies ..."); PSTMT.SetString (1, "The Great Escape"); pstmt.executeUpdate (); // ... conn.commit ();

转载请注明原文地址:https://www.9cbs.com/read-97334.html

New Post(0)