我是初學者,我的理解@Transactional
只需確保所有@Transactional
註釋的類或方法的內部工作將被包裝在一個事務中,所有來自外部來源的通話將創建一個新的交易,但爲什麼我們實際上在下面的Repository中需要這些註釋,以及在普通情況下與readOnly = true
一起使用它的優點是什麼?這是使用彈簧 & 休眠(https://github.com/spring-projects/spring-petclinic)彈簧寵物診所示例應用程序。使用@Transactional(readOnly = true)有什麼好處?
/**
* Repository class for <code>Pet</code> domain objects All method names are compliant with Spring Data naming
* conventions so this interface can easily be extended for Spring Data See here: http://static.springsource.org/spring-data/jpa/docs/current/reference/html/jpa.repositories.html#jpa.query-methods.query-creation
*
* @author Ken Krebs
* @author Juergen Hoeller
* @author Sam Brannen
* @author Michael Isvy
*/
public interface PetRepository extends Repository<Pet, Integer> {
/**
* Retrieve all {@link PetType}s from the data store.
* @return a Collection of {@link PetType}s.
*/
@Query("SELECT ptype FROM PetType ptype ORDER BY ptype.name")
@Transactional(readOnly = true)
List<PetType> findPetTypes();
/**
* Retrieve a {@link Pet} from the data store by id.
* @param id the id to search for
* @return the {@link Pet} if found
*/
@Transactional(readOnly = true)
Pet findById(Integer id);
/**
* Save a {@link Pet} to the data store, either inserting or updating it.
* @param pet the {@link Pet} to save
*/
void save(Pet pet);
}
這是回答您的問題嗎? https://stackoverflow.com/questions/1614139/spring-transactional-read-only-propagation –
你好,如果答案幫助你不要忘記接受/ upvote它。 – Cepr0