What is a transaction, many from only the beginning of Java friends certainly do not know this concept, after all, spring is for enterprise application development is the product of the Java EE specification, so a lot of things, beginners are certainly not heard of.
Because I used to be a web dog. SQL injection is a self-cooked, so the concept of transaction, but it is actually a concept of database operations, whether it is an ORM or JDBC data source, its own database operations are SQL to implement,
SQL mentions a very important concept, that is, the transaction, because the SQL command CRUD database is likely to go wrong, so you have to start a transaction, and then after modifying the query, to determine the success of the modification before committing the transaction, this time the database will persist the data to
Drive, or you can discard the transaction, and the modification will not be persisted to the hard disk.
For the transaction configuration, I read the video in Mu class, better understand and easier to configure is the annotation, here I put the annotated declaration code
PackagePw.jonwinters.demo4;Importorg.springframework.transaction.annotation.Isolation;Importorg.springframework.transaction.annotation.Propagation;Importorg.springframework.transaction.annotation.Transactional;//The propagation behavior isolation level of transactions that are configured for transactions after the annotation is logged off, the transaction is not rolled back and the method for database operations is persisted before the exception is generated .@Transactional (propagation=propagation.required,isolation=Isolation.default) Public classAccountserviceimplImplementsAccountservice {//@Autowired PrivateAccountdao Accountdao; Public voidSetaccountdao (Accountdao Accountdao) { This. Accountdao =Accountdao; } @Override Public voidTransferFinalString out,FinalString in,FinalDouble Money) {Accountdao.outmoney (out, money); //int result = 1/0; Comment out exceptions except 0
Accountdao.inmoney (in, money);//Because there are annotations above, this class will be proxied, even if an exception occurs, the above database-specific operation will not be persisted
} }
<BeanID= "TransactionManager"class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager"> < Propertyname= "DataSource"ref= "DataSource"></ Property> </Bean> <Tx:annotation-drivenTransaction-manager= "TransactionManager" />
Above is configure transaction management, then turn on default note driver scan, configure transaction manager for Datasourcetransactionmanager
Spring Transaction Management