๐ Transactional์ ์ดํด
package hellojpa;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
@Getter
@Setter
public class Member {
@Id
private Long id;
private String name;
}
๐ ๊ธฐ๋ณธ ์ ์ธ JPA์ ์คํ ๋์์๋ฆฌ๋ ์๋์ ๊ฐ์ ๊ทธ๋ฆผ์ผ๋ก ์๋๋๋ค. Persistence๋ผ๋ ํด๋์ค์์ ์์์ ํ ํ ์ค์ ์ ๋ณด๋ฅผ ์กฐํ ํ ํ EntityManagerFactory๋ฅผ ์์ฑํํ ๋์์ด ์์๋๋ง๋ค EntityManager๋ฅผ ์์ฑํ๋ค. ๋จผ์ JPA๋ฅผ ์ด์ฉํ์ฌ Entity๋ฅผ ๊ฐ์ ธ์ค๊ฒ ๋๋ฉด Transactional์ ์ปค๋ฐํ๋ ์์ ์ ์ฒดํฌ ํ ํ ๋ณ๊ฒฝ์ ์ด ์๋ค๋ฉด uptade์ปค๋ฐ์ ๋ง๋ค์ด ๋ ๋ฆฌ๊ณ Transactional์ ์ปค๋ฐํ๋ค.
JPA ์๋ ๋ฐฉ์
- [ ] ์ํฐํฐ ๋งค๋์ ํฉํ ๋ฆฌ๋ฅผ ์์ฑ์์ DB์ฐ๊ฒฐ์ด ๋๋ค.
- [ ] ์ํฐํฐ ๋งค๋์ ํฉํ ๋ฆฌ๋ ์ดํ๋ฆฌ์ผ์ด์ ๋ก๋ฉ ์์ ์ ํ๋๋ง ์์ฑํด์ผ ํ๋ค.
- [ ] ์ํฐํฐ ๋งค๋์ ๋ ํ๋์ Transactional(db connection์ด๋ db์ ๋ณ๊ฒฝ์ ์ด ์๊ธธ๋๋ง๋ค)์ ์งํ์์ ํ๋์ฉ ๋ง๋ค์ด์ผ ํ๋ค.
- [ ] ๊ทธ๋ฆฌ๊ณ Transactional์ ์คํ ํ ๊ฐ์ ์งํํ๋ค.
- [ ] ์ ์ผ ์ค์ํ ๊ฒ์ ์ํฐํฐ ๋งค๋์ ๋ ์๋ฐ ์ปฌ๋ ์ ์ผ๋ก ์๊ฐํ๋ฉด ๋๋ค. ๋ด ์๋ฐ ์ปฌ๋ ์ ์ ๋์ ์ ์ฅํด์ฃผ๋ ์ญํ์ ํด์ค๋ค.
package hellojpa;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
public class JpaMain {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello");
// DB์ ์ปค๋ฅ์
์ ํ๋ ์ป์ ๊ฒ์ด๋ค.
EntityManager em = emf.createEntityManager();
// Transaction ์์
EntityTransaction tx = em.getTransaction();
tx.begin();
Member member = new Member();
member.setId(1L);
member.setName("helloA");
// JPA์ ์ฅ
em.persist(member);
// commit
tx.commit();
em.close();
emf.close();
}
}
๐ก ์ถ๋ ฅ๊ฐ์ ์๋์ ๊ฐ์ผ๋ฉฐ DB๋ฅผ ์กฐํ ํ๊ฒ ๋๋ฉด ์๋์ ๊ฐ์ด ์ฟผ๋ฆฌ๋ฌธ์ ์์ฑํ์ง ์์์์๋ ์ค์ ๋ก ๋ฐ์ดํฐ๋ค์ด DB์ ์ ์ฅ๋๊ฒ ๋๋ค.
<property name="hibernate.show_sql" value="true"/> : ์ฟผ๋ฆฌ์ ๊ฐ์ด ์ค์ ๋ก ์ถ๋ ฅ์ด ๋๊ฒ ํ๋ ๊ตฌ๋ฌธ
<property name="hibernate.format_sql" value="true"/> : ์ฟผ๋ฆฌ๋ฅผ ๋ณด๊ธฐ ์ฝ๊ฒ ํฌ๋ฉงํ
ํ ๊ตฌ๋ฌธ
<property name="hibernate.use_sql_comments" value="true"/> : ์ฟผ๋ฆฌ๊ฐ ๋์จ ์ด์ ๋ฅผ ์๋ ค์ฃผ๋ ๊ตฌ๋ฌธ ์ฃผ์ ์ฒ๋ฆฌ ๋ ๋ถ๋ถ
Hibernate:
/* insert hellojpa.Member
*/ insert
into
Member
(name, id)
values
(?, ?)
ํ์ง๋ง ์ด๋ด ๊ฒฝ์ฐ ์์ธ ์ํฉ์ ๋์ฒ ํ ์ ์๊ธฐ์ ์๋์ ๊ฐ์ ์ฝ๋๋ก ๊ตฌํํ๋ ๊ฒ์ด ์ ์์ด๋ค.
package hellojpa;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
public class JpaMain {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello");
// DB์ ์ปค๋ฅ์
์ ํ๋ ์ป์ ๊ฒ์ด๋ค.
EntityManager em = emf.createEntityManager();
// Transaction ์์
EntityTransaction tx = em.getTransaction();
tx.begin();
try {
Member member = new Member();
member.setId(2L);
member.setName("helloB");
// JPA์ ์ฅ
em.persist(member);
// commit
tx.commit();
} catch (Exception e){
tx.rollback();
} finally {
// EntityManager๊ฐ DB์ปค๋ฅ์
์ ๋ฌผ๊ณ ๋์ํ๊ธฐ์ ์ฌ์ฉ์ ํ๊ณ ๋ซ์์ค์ผ ํ๋ค.
em.close();
}
emf.close();
}
}
ํ์ ์์
package hellojpa;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
public class JpaMain {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello");
// DB์ ์ปค๋ฅ์
์ ํ๋ ์ป์ ๊ฒ์ด๋ค.
EntityManager em = emf.createEntityManager();
// Transaction ์์
EntityTransaction tx = em.getTransaction();
tx.begin();
try {
Member findMember = em.find(Member.class, 1L);
System.out.println(findMember.getId());
System.out.println(findMember.getName());
// commit
tx.commit();
} catch (Exception e){
tx.rollback();
} finally {
// EntityManager๊ฐ DB์ปค๋ฅ์
์ ๋ฌผ๊ณ ๋์ํ๊ธฐ์ ์ฌ์ฉ์ ํ๊ณ ๋ซ์์ค์ผ ํ๋ค.
em.close();
}
emf.close();
}
}
๐ก ์ถ๋ ฅ๊ฐ : ์กฐํ์์ select์ฟผ๋ฆฌ๊ฐ ์๋์ ์ผ๋ก ๋ ์๊ฐ๋ฉฐ ๊ฐ์ ๋ค๊ณ ์ค๋ ๊ฒ์ ํ์ธ ํ ์ ์๋ค.
Hibernate:
select
member0_.id as id1_0_0_,
member0_.name as name2_0_0_
from
Member member0_
where
member0_.id=?
1
helloA
ํ์ ์ญ์
package hellojpa;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
public class JpaMain {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello");
// DB์ ์ปค๋ฅ์
์ ํ๋ ์ป์ ๊ฒ์ด๋ค.
EntityManager em = emf.createEntityManager();
// Transaction ์์
EntityTransaction tx = em.getTransaction();
tx.begin();
try {
Member findMember = em.find(Member.class, 1L);
em.remove(findMember);
// commit
tx.commit();
} catch (Exception e){
tx.rollback();
} finally {
// EntityManager๊ฐ DB์ปค๋ฅ์
์ ๋ฌผ๊ณ ๋์ํ๊ธฐ์ ์ฌ์ฉ์ ํ๊ณ ๋ซ์์ค์ผ ํ๋ค.
em.close();
}
emf.close();
}
}
ํ์ ์ญ์
package hellojpa;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
public class JpaMain {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello");
// DB์ ์ปค๋ฅ์
์ ํ๋ ์ป์ ๊ฒ์ด๋ค.
EntityManager em = emf.createEntityManager();
// Transaction ์์
EntityTransaction tx = em.getTransaction();
tx.begin();
try {
Member findMember = em.find(Member.class, 1L);
findMember.setName("helloJPA");
// commit
tx.commit();
} catch (Exception e){
tx.rollback();
} finally {
// EntityManager๊ฐ DB์ปค๋ฅ์
์ ๋ฌผ๊ณ ๋์ํ๊ธฐ์ ์ฌ์ฉ์ ํ๊ณ ๋ซ์์ค์ผ ํ๋ค.
em.close();
}
emf.close();
}
}
์ถ๋ ฅ ๊ฐ์ ์๋์ ๊ฐ์ด ๋์ค๋ฉฐ ์ปฌ๋ ์ ์ฒ๋ผ ํ์ฉ์ ํ๊ธฐ์ ์ ์ฅํ๋ ๋ฉ์๋๋ ํ์๊ฐ ์๊ฒ ๋๋ค. JPA๋ฅผ ํตํด Entity๋ฅผ ๋ค๊ณ ์ค๊ฒ ๋๋ฉด JPA๊ฐ Transactional์ ์ปค๋ฐํ๋ ์์ ์ ๋ณ๊ฒฝ ์ฌํญ์ด ์๋์ง ์ฒดํฌํ๊ฒ ๋๋ค. ๋ณ๊ฒฝ์ ์ด ์์ ์์ ์๋์ ์ผ๋ก ์ปค๋ฐํ๊ธฐ ์ง์ ์ UPDATE์ฟผ๋ฆฌ๋ฅผ ๋ง๋ค์ด์ ์ฌ์ฉํ๋ค.
Hibernate:
select
member0_.id as id1_0_0_,
member0_.name as name2_0_0_
from
Member member0_
where
member0_.id=?
Hibernate:
/* update
hellojpa.Member */ update
Member
set
name=?
where
id=?
์ฃผ์ ์ฌํญ
- ์น ์๋ฒ๊ฐ ์ฌ๋ผ์ค๋ ์์ DB๋น EntityManagerFactory ๋ ํ๋๋ง ์์ฑ์ด ๋๋ค.
- EntityManager ๋ ๊ณ ๊ฐ์ ์์ฒญ์ด ์ฌ ๋๋ง๋ค ์์ฑ ๋์๋ค๊ฐ ์ญ์ ๋๋ ๋ฐฉ์์ ์ฌ์ฉํ๋ค.
- EntityManager ๋ ์ฐ๋ ๋๋ง๋ค ๊ณต์ ํ ์ ์๊ณ ์ฌ์ฉํ๊ณ ๋ฒ๋ ค์ผ ํ๋ค.
- JPA์ ๋ชจ๋ ๋ฐ์ดํฐ์ ๋ณ๊ฒฝ์ Transaction ์์์๋ง ์คํ๋๋ค.
๐ก JPQL : ๋ง์ Table์ค์์ ๋ด๊ฐ ์ํ๋ ๋ฐ์ดํฐ๋ค์ ์ด๋ป๊ฒ ์กฐํ ํ ๊ฒ์ธ์ง๋ฅผ ํด๊ฒฐํ๊ฒ ํด์ฃผ๋ ๋ฐฉ์
- ๊ฒ์ ์ฟผ๋ฆฌ์ ๊ฒฝ์ฐ ๋ฐ์ดํฐ๋ฅผ ๊ฒ์์ ํด์ ๋ค๊ณ ์์ผ ํ๋ค. ํ์ง๋ง ํ ์ด๋ธ์ ๊ฒ์์์ ๋ฌธ๋ฒ์ด ์ด๊ธ๋๋ฏ๋ก ์ํฐํฐ ๊ฐ์ฒด๋ฅผ ๋์์ผ๋ก ๊ฒ์์ ํด์ผ ํ๋ค.
- ๊ฒ์ ์กฐ๊ฑด์ด ํฌํจ๋ SQL์ด ํ์ํ๋ฐ ์ค์ ๋ฌผ๋ฆฌ์ ์ธ ํ ์ด๋ธ์ธ RDB์ ์ฟผ๋ฆฌ๋ฅผ ๋ ๋ฆฐ๋ค๋ฉด ํด๋น ๋๋น์ ์ข ์์ ์ธ ์ค๊ณ๊ฐ ๋๋ฒ๋ฆฐ๋ค. ๊ทธ๋ ๊ธฐ์ ์ํฐํฐ ๊ฐ์ฒด๋ฅผ ๋์์ผ๋ก ์ฌ์ฉํ ์ ์๋ ์ฟผ๋ฆฌ๊ฐ ์ ๊ณต๋๋ค.
๐ก ์คํ์์ ํ์ฌ ๋ฐ์ดํฐ์ ๋ง๋ ์ ์ ํ ์ฟผ๋ฆฌ๋ฌธ์ด ์์ฑ๋๋ค.
๐๏ธํต์ฌ ๋ด์ฉ
- ๋ชจ๋ ๋ณ๊ฒฝ์ Transaction ์์์ ์คํ๋๋ฉฐ ์กฐํ ๊ฐ์ ๋ฐ์ดํฐ์ ๊ฐ์ ๋ณ๊ฒฝํ์ง ์๋ ๋ฉ์๋๋ Transaction ์ ์ ์ธํ์ง ์์๋(์์ด์๋ ์ด์ ๋ ๋ฒจ์์ ์ ๋ ์ง๋ ๋ชจ๋ฅธ๋ค ์ด ๋ถ๋ถ ์ํ)์ ์๋ํ๋ค. Transaction ์์๊ณผ ์ปค๋ฐ์ ๋ด๋นํ๋ค.
- Transaction commit์ ํ์ง ์๋๋ค๋ฉด ๋ฐ์์ด ๋์ง ์๋๋ค.
- EntityManager ๋ ํ๋์ ๋์์ด ๋๋ ์ ๋ซ๊ณ ๋ค๋ฅธ ๋์์ด ๋ฐ์์ ์๋์ ์ผ๋ก ์์ฑ๋๋ค.
- WAS๊ฐ ๋ด๋ ค๊ฐ ๊ฒฝ์ฐ ManagerFactory ๋ฅผ ๋ซ์์ค์ผ๋ง ์ปค๋ฅ์
ํ๋ง ๊ฐ์ ๋ถ๋ถ์ ๋ฆฌ์์ค๊ฐ ํด์ฒด๋๊ฒ ๋๋ค.
์ถ์ฒ : https://inf.run/spjG
'JPA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ค์ํ ์ฐ๊ด๊ด๊ณ ๋งคํ (section 6) (0) | 2022.08.23 |
---|---|
์ฐ๊ด๊ด๊ณ ๋งคํ ๊ธฐ์ด (section 5) (0) | 2022.08.16 |
์ํฐํฐ ๋งคํ(section 4) (0) | 2022.08.14 |
์์์ฑ ์ปจํ ์คํธ(section 3) (1) | 2022.08.09 |
JPA์ ์๊ฐ(section1) (0) | 2022.08.05 |