I'm having an odd issue during record saving to database.
It sometimes create exactly duplicate record except the UUID part even though the function is only run once.
For the last 6 months I only see this happen twice and I got no clue what is happening.
User Entity:
@Entity
@Table(name = "table_user")
public class User extends Deletable {
@Id
@Column(name = "id")
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid2")
private String id;
@Column(name = "name")
private String name;
@Column(name = "phone_number")
private String phoneNumber;
@Column(name = "job_title")
private String jobTitle;
@Column(name = "email")
private String email;
@Column(name = "password")
private String password;
@Column(name = "auth_key")
private String authKey;
}
Role Entity:
@Data
@Entity
@Table(name = "table_role")
public class Role extends Deletable {
@Id
@Column(name = "id")
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid2")
private String id;
@Column(name = "name")
private String name;
@Column(name = "type")
private String type;
}
User Role Entity
@Data
@Entity
@Table(name = "table_user_role")
public class UserRole {
@Id
@Column(name = "id")
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid2")
private String id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "role_id")
private Role role;
}
Creating User Function on Service:
@Override
public User save(UserDto userDto) {
String emailRegex = "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z0-9]+";
if (!StringUtils.hasValue(userDto.getEmail())) {
throw new RuntimeException("Please enter Email");
}
if (userRepository.findByEmail(userDto.getEmail()).isPresent()) {
throw new RuntimeException("Email already registered");
}
User user = new User();
user.setProfilePictureUrl(user.getProfilePictureUrl());
user.setName(userDto.getName());
user.setAutoRefresh(userDto.getAutoRefresh());
user.setJobTitle(userDto.getJobTitle());
user.setPhoneNumber(userDto.getPhoneNumber());
if (!Pattern.matches(emailRegex, userDto.getEmail())) {
throw new RuntimeException("Invalid Email Format");
}
user.setEmail(userDto.getEmail());
if (StringUtils.hasValue(userDto.getPassword())) {
user.setPassword(bCryptPasswordEncoder.encode(userDto.getPassword()));
} else {
user.setPassword(bCryptPasswordEncoder.encode(RandomStringUtils.randomAlphanumeric(12)));
}
String authKey = StringUtils.randomString();
while (userRepository.findByAuthKeyEquals(authKey).isPresent()) {
authKey = StringUtils.randomString();
}
user.setAuthKey(authKey);
user.setCreatedAt(OffsetDateTime.now());
if (null != UserContext.getUserId()) {
user.setCreatedBy(UserContext.getUserId());
user.setCreatorUsername(UserContext.getUserName());
}
userRepository.save(user);
if (StringUtils.hasValueListOfString(userDto.getRoleList())) {
for (String role : userDto.getRoleList()) {
UserRole userRole = new UserRole();
Role roleCd = roleService.findById(role).orElseThrow(() -> new RuntimeException("No Role found with Id : " + role));
userRole.setUser(user);
userRole.setRole(roleCd);
userRoleService.save(userRole);
}
} else {
throw new RuntimeException("Please assign proper Role for new User");
}
return user;
}