Java Persistence API
...
Transaction management refers to the tasks of processing multiple transactions issued by various clients of a database server in such a way that the ACID contract can be fulfilled, that is, the properties of atomicity, consistency preservation, isolation, and durability of each individual transaction can be guaranteed. Transaction management is generally understood as requiring serializability-based concurrency control as well as recovery from failures. Concurrency control is the task of scheduling transactions such that their serializability can be guaranteed, while recovery has to restore a consistent database state after a system or media failure. Assuming that the database server is in charge of the “C,” the former guarantees the “I” in ACID, the latter the “A” and “D” properties. Transaction management has to be highly efficient, as modern transaction servers need to accommodate thousands of transactions...
The Spring Framework provides a consistent abstraction for transaction management. The Spring Framework’s declarative transaction management is made possible with Spring aspect-oriented programming (AOP), although, as the transactional aspects code comes with the Spring Framework distribution and may be used in a boilerplate fashion, AOP concepts do not generally have to be understood to make effective use of this code.
Source: https://docs.spring.io/spring-framework/docs/5.3.7/reference/html/data-access.html#transaction
Aspect-Oriented Programming
Aspect-Oriented Programming (AOP) complements Object-Oriented Programming (OOP) by providing another way of thinking about program structure. The key unit of modularity in OOP is the class, whereas in AOP the unit of modularity is the aspect. Aspects enable the modularization of concerns such as transaction management that cut across multiple types and objects. (Such concerns are often termed crosscutting concerns in AOP literature.)
One of the key components of Spring is the AOP framework. While the Spring IoC (Inversion of control) container does not depend on AOP, meaning you do not need to use AOP if you don’t want to, AOP complements Spring IoC to provide a very capable middleware solution.
Source: https://docs.spring.io/spring-framework/docs/5.3.7/reference/html/core.html#aop
Use a Higher-level Database Migration Tool
Spring Boot supports two higher-level migration tools: Flyway and Liquibase.
Example how how to set things up with Liquibase: https://github.com/spring-projects/spring-boot/tree/v2.1.18.RELEASE/spring-boot-samples/spring-boot-sample-liquibase
...
Using Spring Data JPA it needs to implement a repository for each model.
The goal of Spring Data repository abstraction is to significantly reduce the amount of boilerplate code required to implement data access layers for various persistence stores.
Source: https://docs.spring.io/spring-data/data-commons/docs/2.5.1/reference/html/#repositories
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
@Repository public interface JpaToscaPropertyRepository extends JpaRepository<JpaToscaProperty, PfReferenceKey> { } |
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
@ExtendWith(SpringExtension.class) @DataJpaTest @Import(value = ParticipantPolicyParameters.class) @TestPropertySource(locations = {"classpath:application_test.properties"}) class JpaToscaPropertyRepositoryTest { @Autowired private JpaToscaPropertyRepository toscaPropertyRepository; @Test void test() { JpaToscaProperty toscaProperty = new JpaToscaProperty(); PfReferenceKey key = toscaProperty.getKey(); Map<String, String> metadata = new HashMap<>(); metadata.put("Key", "Value"); metadata.put("K", "V"); List<JpaToscaConstraint> constraints = new ArrayList<>(); String[] list = new String[] {"First", "Second"}; constraints.add(new JpaToscaConstraintValidValues(Stream.of(list).collect(Collectors.toList()))); toscaProperty.setDefaultValue("DefaultValue"); toscaProperty.setDescription("Description"); toscaProperty.setRequired(true); toscaProperty.setStatus(ToscaProperty.Status.EXPERIMENTAL); toscaProperty.setMetadata(metadata); toscaProperty.setConstraints(constraints); toscaPropertyRepository.save(toscaProperty); Optional<JpaToscaProperty> opt = toscaPropertyRepository.findById(key); assertThat(opt).isNotEmpty(); JpaToscaProperty actual = opt.get(); assertThat(actual.getDefaultValue()).isEqualTo(toscaProperty.getDefaultValue()); assertThat(actual.getDescription()).isEqualTo(toscaProperty.getDescription()); assertThat(actual.isRequired()).isEqualTo(toscaProperty.isRequired()); assertThat(actual.getStatus()).isEqualTo(toscaProperty.getStatus()); assertThat(actual.getType()).isEqualTo(toscaProperty.getType()); assertThat(actual.getConstraints()).isEqualTo(toscaProperty.getConstraints()); } } |
Using Dao
There is a way to use the whole Policy Framework implementation. It needs to create a new DefaultPfDao class that uses the Entity Manger with no "begin transaction" and no "commit".
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
public class DefaultPfDao implements PfDao {
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultPfDao.class);
private final EntityManager entityManager;
/**
* Constructor.
*
* @param entityManager EntityManager
*/
public DefaultPfDao(EntityManager entityManager) {
this.entityManager = entityManager;
}
@Override
public void init(final DaoParameters daoParameters) throws PfModelException {
// Not need
}
@Override
public <T extends PfConcept> void create(final T obj) {
if (obj == null) {
return;
}
entityManager.merge(obj);
}
@Override
public <T extends PfConcept> void delete(final T obj) {
entityManager.remove(entityManager.contains(obj) ? obj : entityManager.merge(obj));
}
|
EntityManager is not thread safe, and it need to use @PersistenceContext annotation, so Spring will inject a thread-safe proxy for the actual transactional EntityManager.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
@Service
@Transactional
public class PolicyModelsProviderImpl implements PolicyModelsProvider {
@PersistenceContext
private EntityManager entityManager;
@Override
public ToscaServiceTemplate createServiceTemplate(@NonNull ToscaServiceTemplate serviceTemplate) {
LOGGER.debug("->createServiceTemplate: serviceTemplate={}", serviceTemplate);
try {
ToscaServiceTemplate createdServiceTemplate =
new SimpleToscaProvider().appendToServiceTemplate(new DefaultPfDao(entityManager),
new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();
LOGGER.debug("<-createServiceTemplate: createdServiceTemplate={}", createdServiceTemplate);
return createdServiceTemplate;
} catch (Exception pfme) {
throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, pfme.getMessage(), pfme);
}
}
|
Spring Service
Example how to convert ControlLoopInstantiationProvider class in Spring style using "@Service" and "@Transactional"
...