Spring boot transaction mechanism
Spring supports declarative transactions, with @tracsational annotations indicating that the method requires transactional support. The annotated method opens a new transaction when it is invoked, and spring submits the transaction when the method has no exception.
Properties: Propagation, defining the life cycle of a transaction, isolation, isolating, determining the integrity of a transaction, timeout, transaction expiration time, ReadOnly, read-only transaction, rollback, specifying which exceptions can cause a transaction to be rolled back; Norollback , which exceptions cannot cause a transaction to be rolled back.
Spring data JPA opens transaction support for all default methods.
1. Entity class Person
2.repository Solid Class Personrepository
3.service:
public class Demoserviceimpl implements demoservice{
@Autowired
Personrepository personrepository;
@Transactional (rollbackfor={illegalargumentexception.class}
Public person Savewithrollback (person p) {
Person p = Personrepository.save (p);
throw new IllegalArgumentException ("rollback");
return p;
}
@Transactional (norollbackfor={illegalargumentexception.class}
Public person Savewithoutrollback (person p) {
Person p = Personrepository.save (p);
throw new IllegalArgumentException ("rollback");
return p;
}
4.controller
5. Test:http://localhost:8080/rollback?name=tom-> Console throws an exception, the database does not have new records
Http://localhost:8080/norollback?name=tom-> Console throws an exception, the database has new records
Spring Cache
CacheManager is a variety of cache technology abstraction interfaces provided by spring, and the cache interface contains various operations for caching. For different caching techniques, different cachemanager, such as Simplecachemanager (using simple collection cache data, primarily for testing purposes), need to be implemented; Rediscachemanager ...
Note: The @cacheable to see the cache before execution, there is data directly returned, there is no data call method and put the return value into the cache;
@cachePut will put the return value of the method into the cache regardless of how
@cacheevict Delete one or more data from the cache
@caching combine multiple annotation policies on a single method
@cacheable, @cachePut, @cacheevict have the Value property: The name of the cache to use; Key property: The keys that data stores in the cache.
eg. public class Demoserviceimpl {
@Autowired
Personrepository personrepository;
@Override
@CachePut (value= "People", key= "#person. ID")
Public person Save (person p) {
Person p = Personrepository.save (p);
return p;
}
}
Spring boot (quad) transactions and caching