wiki:Transactions

Version 1 (modified by 221007, 8 days ago) ( diff )

--

Трансакции

Употребата на трансакции беше од голема корист за операции кои требаа да се извршат или сите или да падне, за лесно да се најде грешката. Во мојата апликација истото го употребив во неколку наврати, кои ќе ги покажам подолу.

1. Трансакција при регистрација на нов корисник

 @Transactional
    public void register_user(String name, String surname, String email, String password, String password_confirmation) {
        if (email == null || email.isEmpty() || password == null || password.isEmpty() || password_confirmation == null || password_confirmation.isEmpty()) {
            throw new NoExistingCredentialsException("Credentials cannot be empty.");
        }
        if (!password.equals(password_confirmation)) {
            throw new IllegalArgumentException("Passwords do not match. Check the mistake and try again.");
        }
        if (reportiumUserRepository.findByEmail(email).isPresent()) {
            throw new UserAlreadyExistsException(String.format("User with this email '%s' already exists.", email));
        }
        //creating the user
        ReportiumUser new_application_user = new ReportiumUser();
        new_application_user.setActive(true);
        new_application_user.setName(name);
        new_application_user.setSurname(surname);
        new_application_user.setEmail(email);
        new_application_user.setPasswordHash(passwordEncoder.encode(password));
        new_application_user.setCreatedAt(LocalDateTime.now());
        ReportiumUser savedUser = reportiumUserRepository.save(new_application_user);
        //I have a trigger that creates the user profile
        UserProfile user_profile = userProfileRepository.findByReportiumUser(savedUser).get();
        UserProfileLog userProfileLog = new UserProfileLog();
        userProfileLog.setUserProfile(user_profile);
        userProfileLog.setChangedAt(LocalDateTime.now());
        String description = String.format("New user <%s %s> with mail '%s' has been registered.", savedUser.getName(), savedUser.getSurname(), savedUser.getEmail());
        userProfileLog.setChangeDescription(description);
        profileLogRepository.save(userProfileLog);
    }

2.

3.

Note: See TracWiki for help on using the wiki.