Spring boot (quad) transactions and caching

Source: Internet
Author: User

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

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.