在我的Spring启动应用程序中,我为不同的对象(例如组,用户等)提供了多个服务层。所有这些服务层都是@Transactional。
我将JPA用于我的存储库:
public interface InvitationRepository extends JpaRepository<Invitation, Long> {
public Invitation findById(long id);
}
在服务层中,我有以下方法:
public void accept(long invitationId, String username) {
Invitation invitation = this.loadById(invitationId);
User invitee = userService.loadByUsername(username);
Group group = invitation.getGroup();
groupService.addMember(group.getId(), invitee.getId());
invitee.addMemberGroup(group);
invitee.removeReceivedInvitation(invitation);
User inviter = userService.loadById(invitation.getInviter().getId());
inviter.removeOwnedInvitation(invitation);
invitationRepository.delete(invitation);
}
我在查看数据库时调试了这段代码。它应该工作正常,但数据库没有变化。尽管我的服务层使用了@Transactional,但我没有看到数据库中的变化的原因是什么?
更新1
@Configuration
public class ProductionDataSourceConfiguration {
@Bean
@Primary
@Profile("!test")
public BasicDataSource productionDataSource() throws URISyntaxException {
URI dbUri = new URI(System.getenv("DATABASE_URL"));
String username = dbUri.getUserInfo().split(":")[0];
String password = dbUri.getUserInfo().split(":")[1];
String dbUrl = String.format("jdbc:mysql://%s%s?useSSL=false", dbUri.getHost(), dbUri.getPath());
BasicDataSource basicDataSource = new BasicDataSource();
basicDataSource.setUrl(dbUrl);
basicDataSource.setUsername(username);
basicDataSource.setPassword(password);
return basicDataSource;
}
}