お世話になります。
トランザクションを使用したデータの操作で、
コミット前のデータが取得できない場合とできる場合があるのですが、
原因がよくわからないため、ご教示頂けますと幸いです。
各バージョン情報 Java:1.8 フレームワーク:Spring Boot ORM:JPA ORMバージョン:spring-boot-starter-data-jpa-2.0.0.BUILD-SNAPSHOT DB:Oracle DBバージョン:12.2.0.1.0
①下記のような場合、コミット前のデータ(今からコミットしようとしているデータ)が取得できます。
//Controller TransactionDefinition def = new DefaultTransactionDefinition(); TransactionStatus status = transactionManager.getTransaction(def); try { hogeRepository.updateHoge( form.getId(), form.getComment() ); } catch(Exception e) { transactionManager.rollback(status); } //上記(updateHoge)で流したコミット前のデータ(DBに反映されていない最新のデータ)を取得できる。 Hoge hoge = hogeRepository.findById(form.getId()); transactionManager.commit(status);
②updateHogeの前にfindメソッドがあると、古いデータしか取得できない。
//Controller TransactionDefinition def = new DefaultTransactionDefinition(); TransactionStatus status = transactionManager.getTransaction(def); //updateHogeメソッド前にfindByIdメソッドがあると、updateHoge実行後にnewHogeの最新データが入らない。 Hoge oldHoge = hogeRepository.findById(form.getId()); try { hogeRepository.updateHoge( form.getId(), form.getComment() ); } catch(Exception e) { transactionManager.rollback(status); } //oldHogeと同じデータしか取得できない。 Hoge newHoge = hogeRepository.findById(form.getId()); transactionManager.commit(status);
//Repository public interface HogeRepository extends JpaRepository<Hoge, Long>, JpaSpecificationExecutor<Hoge> { @Modifying @Query("UPDATE Hoge SET comment=:comment WHERE id = :id") Integer updateHoge(@Param("comment") String comment, @Param("id") Long id); }
・試してみたこと①
TransactionDefinition def = new DefaultTransactionDefinition(); TransactionStatus status = transactionManager.getTransaction(def); try { hogeRepository.updateHoge( form.getId(), form.getComment() ); //コミット前のデータが取得できました。 Hoge hoge1 = hogeRepository.findById(form.getId()); } catch (Exception e) { } //コミット前のデータが取得できました。 Hoge hoge2 = hogeRepository.findById(form.getId()); try { //コミット前のデータが取得できました。 Hoge hoge3 = hogeRepository.findById(form.getId()); hogeRepository.updateHoge( form.getId(), form.getComment() ); //コミット前のデータが取得できました。 Hoge hoge4 = hogeRepository.findById(form.getId()); } catch (Exception e) { } //コミット前のデータが取得できました。 Hoge hoge5 = hogeRepository.findById(form.getId());
試してみたこと②(試してみたこと①の処理を逆にしてみました)
TransactionDefinition def = new DefaultTransactionDefinition(); TransactionStatus status = transactionManager.getTransaction(def); try { //古いデータが取得されました。 Hoge hoge3 = hogeRepository.findById(form.getId()); hogeRepository.updateHoge( form.getId(), form.getComment() ); //古いデータが取得されました。 Hoge hoge4 = hogeRepository.findById(form.getId()); } catch (Exception e) { } //古いデータが取得されました。 Hoge hoge5 = hogeRepository.findById(form.getId()); try { hogeRepository.updateHoge( form.getId(), form.getComment() ); //古いデータが取得されました。 Hoge hoge1 = hogeRepository.findById(form.getId()); } catch (Exception e) { } //古いデータが取得されました。 Hoge hoge2 = hogeRepository.findById(form.getId());
試してみたこと③
TransactionDefinition def = new DefaultTransactionDefinition(); TransactionStatus status = transactionManager.getTransaction(def); try { //古いデータが取得されました。 Hoge hoge1 = hogeRepository.findById(form.getId()); hoge1.setComment(form.getComment()); hoge1.save(); //コミット前のデータが取得されました。 Hoge hoge2 = hogeRepository.findById(form.getId()); } catch (Exception e) { }
回答2件
あなたの回答
tips
プレビュー