Home > Back-end >  Write business logic Service layer, dao layer is the smallest unit of database operations, the trans
Write business logic Service layer, dao layer is the smallest unit of database operations, the trans

Time:09-19

Have a doubt, if it's not framework development, database using JDBC operation, then obtain the connection can only be written in the service method? An employee such as a new company, the employee list add a record, the number of personnel department list field value to add one, it involves the operation of the two tables, staff corresponding YuanGongDaoImpl table, department table corresponding BuMenDaoImpl.
The Service layer code have to write like this:

The class xxxServiceImpl {

Private YuanGongDao ygd=omitted;
Privaet BuMenDao BMD=omitted;

Void ZengJiaYuanGong (YuanGong yg) {
//get connection
The Connection con=omitted;
//open transaction
con.setAutoCommit(false);

Ygd. Insert (yg);//new employee records
BMD, update (String SQL);//department staff number plus one

con.commit();
con.close();

}
}
But the above con't incoming ygd. Insert (yg) and BMD, update (String SQL), so these two methods can't to the database operation, if these two methods in their new a new con, that the transaction could not operation, if that's the way ygd. Insert (yg, con ") and (BMD), update (String SQL, con), as if no one such design!
I see a lot of online tutorials, database operations, is the connection, or sqlsession designed to get in on the dao layer, of course, those examples are the database to add and delete, simple operation, I don't understand, if you want to use transaction, how to design ah, don't consider using framework!


CodePudding user response:

Suddenly thought of, if con storage to the thread in the service layer, and then to the dao layer, it is feasible?

CodePudding user response:

reference 1st floor whereismymindM response:
suddenly thought of, if con storage to the thread in the service layer, and then to the dao layer, is that possible?

Spring's transaction management is to save the connection as a ThreadLocal, only yourself is not easy to write good also, such as service call connection where access to service you, where to submit?

CodePudding user response:

refer to the original poster whereismymindM response:
, there is a doubt, if it's not framework development, database using JDBC operation, then obtain the connection can only be written in the service method? An employee such as a new company, the employee list add a record, the number of personnel department list field value to add one, it involves the operation of the two tables, staff corresponding YuanGongDaoImpl table, department table corresponding BuMenDaoImpl.
The Service layer code have to write like this:
But the above con't incoming ygd. Insert (yg) and BMD, update (String SQL), so these two methods can't to the database operation, if these two methods in their new a new con, that the transaction could not operation, if that's the way ygd. Insert (yg, con ") and (BMD), update (String SQL, con), as if no one such design!
I see a lot of online tutorials, database operations, is the connection, or sqlsession designed to get in on the dao layer, of course, those examples are the database to add and delete, simple operation, I don't understand, if you want to use transaction, how to design ah, don't consider using framework!


If no framework, you in addition to the dao design within normal to add, delete, query, update method outside you need to design a link database method, framework bean injection, by link database, you need not framework you need to write your own,

CodePudding user response:

Do not use the framework, typically define a Connection tool, to obtain a Connection, execute SQL, obtain the result set, perform after release Connection, open transaction is set aotucommit thing,

CodePudding user response:

In most cases is a connection, a SQL autocommit things, write their own a DBUtil can fix,
Submitted together with multiple SQL connection, a thing, you can refer to the following code:
 
/* *
* batch execute multiple SQL
*
* @ param connection database connection
* @ param SQLS multiple update statement
*/
Public static void moreSql (Connection Connection, JSONArray SQLS) {
The Statement Statement=null;
Try {
connection.setAutoCommit(false);
statement=connection.createStatement();
For (Object SQL: SQLS) {
if (null !=SQL & amp; & ! ToolUtil. IsBlank (SQL. The toString ())) {
SqlUtil. Show (SQL. The toString (), null, null);
Statement. The addBatch (SQL. The toString ());
}
}
The statement. ExecuteBatch ();//update
Connection.com MIT ();//unity to commit the transaction
The statement. ClearBatch ();//remove the batch
} the catch (Exception e) {
Try {
Connection. The rollback ();//transaction rollback
{} catch Exception (e2)
}
throw new RuntimeException(e);
} the finally {
ReflectUtil. Close (connection, the statement);
}
}

CodePudding user response:

You are in the dao layer added a method to remove the access connection transaction in ServiceImpl layer to obtain