<?xml version="1.0" encoding="UTF-8"?> <document xmlns="http://maven.apache.org/XDOC/2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/XDOC/2.0 http://maven.apache.org/xsd/xdoc-2.0.xsd"> <properties> <title>トランザクション</title> </properties> <body> <section name="目次"> <ul> <li><a href="#概è¦">概è¦</a></li> <li><a href="#è¨å®šæ–¹æ³•">è¨å®šæ–¹æ³•</a></li> <li><a href="#利用方法">利用方法</a> <ul> <li><a href="#ãƒãƒ¼ã‚«ãƒ«ãƒˆãƒ©ãƒ³ã‚¶ã‚¯ã‚·ãƒ§ãƒ³ã®é–‹å§‹ã¨çµ‚了">ãƒãƒ¼ã‚«ãƒ«ãƒˆãƒ©ãƒ³ã‚¶ã‚¯ã‚·ãƒ§ãƒ³ã®é–‹å§‹ã¨çµ‚了</a></li> <li><a href="#セーブãƒã‚¤ãƒ³ãƒˆ">セーブãƒã‚¤ãƒ³ãƒˆ</a></li> </ul> </li> </ul> </section> <section name="概è¦"> <p> Domaã¯ã€ãƒãƒ¼ã‚«ãƒ«ãƒˆãƒ©ãƒ³ã‚¶ã‚¯ã‚·ãƒ§ãƒ³ã‚’サãƒãƒ¼ãƒˆã—ã¾ã™ã€‚ã“ã®ãƒ‰ã‚ュメントã§ã¯ã€ãƒãƒ¼ã‚«ãƒ«ãƒˆãƒ©ãƒ³ã‚¶ã‚¯ã‚·ãƒ§ãƒ³ã®è¨å®šæ–¹æ³•ã¨åˆ©ç”¨æ–¹æ³•ã«ã¤ã„ã¦èª¬æ˜Žã—ã¾ã™ã€‚ </p> <p> ã‚°ãƒãƒ¼ãƒãƒ«ãƒˆãƒ©ãƒ³ã‚¶ã‚¯ã‚·ãƒ§ãƒ³ã‚’使用ã—ãŸã„å ´åˆã¯ã€JTA(Java Transaction API)ã®å®Ÿè£…ã‚’ã‚‚ã¤ãƒ•ãƒ¬ãƒ¼ãƒ ワークやアプリケーションサーãƒãƒ¼ã®æ©Ÿèƒ½ã‚’利用ã—ã¦ãã ã•ã„。 </p> <p> ãƒãƒ¼ã‚«ãƒ«ãƒˆãƒ©ãƒ³ã‚¶ã‚¯ã‚·ãƒ§ãƒ³ã®ã‚µãƒ³ãƒ—ルã¯<a href="../tutorial/index.html">ãƒãƒ¥ãƒ¼ãƒˆãƒªã‚¢ãƒ«</a>を〠JTAを使ã£ãŸã‚°ãƒãƒ¼ãƒãƒ«ãƒˆãƒ©ãƒ³ã‚¶ã‚¯ã‚·ãƒ§ãƒ³ã®ã‚µãƒ³ãƒ—ルã¯<a href="../jpetstore.html">Doma JPetStore</a>ã‚’å‚ç…§ãã ã•ã„。 </p> </section> <section name="è¨å®šæ–¹æ³•"> <p> ãƒãƒ¼ã‚«ãƒ«ãƒˆãƒ©ãƒ³ã‚¶ã‚¯ã‚·ãƒ§ãƒ³ã‚’有効ã«ã™ã‚‹ã«ã¯ã€ãƒ‡ãƒ¼ã‚¿ã‚½ãƒ¼ã‚¹ã¨ã—ã¦<a href ="../apidocs/org/seasar/doma/jdbc/tx/LocalTransactionalDataSource.html"><code>LocalTransactionalDataSource</code></a>を使用ã—ã¾ã™ã€‚ ã“ã®ã‚¯ãƒ©ã‚¹ã®ã‚¤ãƒ³ã‚¹ã‚¿ãƒ³ã‚¹ã¯<a href="config.html">è¨å®šã‚¯ãƒ©ã‚¹</a>ã§ã‚·ãƒ³ã‚°ãƒ«ãƒˆãƒ³ã¨ã—ã¦ç®¡ç†ã—ã€<code>getDataSource()</code>メソッドã§å¤–部ã«è¿”ã™ã‚ˆã†ã«ã—ã¦ãã ã•ã„。 </p> <p> <code>LocalTransactionalDataSource</code>ã®<code>getLocalTransaction(JdbcLogger)</code>メソッドã¯ã€<a href ="../apidocs/org/seasar/doma/jdbc/tx/LocalTransaction.html"><code>LocalTransaction</code></a>を生æˆã—ã¦è¿”ã—ã¾ã™ã€‚ ã“ã®ã‚¯ãƒ©ã‚¹ã®ã‚¤ãƒ³ã‚¹ã‚¿ãƒ³ã‚¹ãŒãƒãƒ¼ã‚«ãƒ«ãƒˆãƒ©ãƒ³ã‚¶ã‚¯ã‚·ãƒ§ãƒ³ã‚’表ã—ã¾ã™ã€‚ ã“ã®ã‚¤ãƒ³ã‚¹ã‚¿ãƒ³ã‚¹ã‚’外部ã¸è¿”ã™ãŸã‚ã®ãƒ¡ã‚½ãƒƒãƒ‰ã‚’è¨å®šã‚¯ãƒ©ã‚¹ã«ä½œæˆã—ã¦ãã ã•ã„。 </p> <p> ãƒãƒ¼ã‚«ãƒ«ãƒˆãƒ©ãƒ³ã‚¶ã‚¯ã‚·ãƒ§ãƒ³ã«å¯¾å¿œã—ãŸè¨å®šã‚¯ãƒ©ã‚¹ã®ä¾‹ã¯ä»¥ä¸‹ã®ã¨ãŠã‚Šã§ã™ã€‚ </p> <source><![CDATA[public class AppConfig extends DomaAbstractConfig { protected static final LocalTransactionalDataSource dataSource = createDataSource(); protected static final Dialect dialect = new HsqldbDialect(); @Override public DataSource getDataSource() { return dataSource; } @Override public Dialect getDialect() { return dialect; } protected static LocalTransactionalDataSource createDataSource() { SimpleDataSource dataSource = new SimpleDataSource(); dataSource.setUrl("jdbc:hsqldb:mem:tutorial"); dataSource.setUser("sa"); return new LocalTransactionalDataSource(dataSource); } public static LocalTransaction getLocalTransaction() { return dataSource.getLocalTransaction(defaultJdbcLogger); } }]]></source> </section> <section name="利用方法"> <p> è¨å®šã‚¯ãƒ©ã‚¹ã‚’Daoインタフェースã«ä»¥ä¸‹ã®ã‚ˆã†ã«æ³¨é‡ˆã™ã‚‹ã¨ã—ã¦ä¾‹ã‚’示ã—ã¾ã™ã€‚ </p> <source><![CDATA[@Dao(config = AppConfig.class) public interface EmployeeDao { ... }]]></source> <subsection name="ãƒãƒ¼ã‚«ãƒ«ãƒˆãƒ©ãƒ³ã‚¶ã‚¯ã‚·ãƒ§ãƒ³ã®é–‹å§‹ã¨çµ‚了"> <p> è¨å®šã‚¯ãƒ©ã‚¹ã‹ã‚‰<code>LocalTransactional</code>ã‚’å–å¾—ã—ã€ãƒˆãƒ©ãƒ³ã‚¶ã‚¯ã‚·ãƒ§ãƒ³ã‚’<code>begin()</code>ã§é–‹å§‹ã—ã€<code>commit()</code>ã¾ãŸã¯<code>rollback()</code>ã§ãƒˆãƒ©ãƒ³ã‚¶ã‚¯ã‚·ãƒ§ãƒ³ã‚’終了ã—ã¾ã™ã€‚ </p> <source><![CDATA[LocalTransaction tx = AppConfig.getLocalTransaction(); try { // 開始 tx.begin(); Employee employee = dao.selectById(1); employee.setName("hoge"); employee.setJobType(JobType.PRESIDENT); dao.update(employee); // コミット tx.commit(); } finally { // ãƒãƒ¼ãƒ«ãƒãƒƒã‚¯ tx.rollback(); }]]></source> <p> 次ã®ã‚ˆã†ã«è¨˜è¿°ã™ã‚‹ã“ã¨ã‚‚ã§ãã¾ã™ã€‚ </p> <source><![CDATA[try { // 開始 AppConfig.getLocalTransaction().begin(); Employee employee = dao.selectById(1); employee.setName("hoge"); employee.setJobType(JobType.PRESIDENT); dao.update(employee); // コミット AppConfig.getLocalTransaction().commit(); } finally { // ãƒãƒ¼ãƒ«ãƒãƒƒã‚¯ AppConfig.getLocalTransaction().rollback(); }]]></source> <p> <code>begin()</code>ã§é–‹å§‹ã•ã‚ŒãŸãƒˆãƒ©ãƒ³ã‚¶ã‚¯ã‚·ãƒ§ãƒ³ã¯ã€å¿…ãš<code>commit()</code>ã¾ãŸã¯<code>rollback()</code>ã§çµ‚了ã—ãªã‘ã‚Œã°ã„ã‘ã¾ã›ã‚“。 <code>LocalTransactionalDataSource</code>を利用ã™ã‚‹ãƒ‡ãƒ¼ã‚¿ã‚¢ã‚¯ã‚»ã‚¹ã¯ã€ä¾‹å¤–ãªãå¿…ãšãƒãƒ¼ã‚«ãƒ«ãƒˆãƒ©ãƒ³ã‚¶ã‚¯ã‚·ãƒ§ãƒ³ã®ä¸ã§å®Ÿè¡Œã—ãªã‘ã‚Œã°ã„ã‘ã¾ã›ã‚“。 </p> <p> トランザクションã¯ãƒˆãƒ©ãƒ³ã‚¶ã‚¯ã‚·ãƒ§ãƒ³åˆ†é›¢ãƒ¬ãƒ™ãƒ«ã‚’指定ã—ã¦é–‹å§‹ã™ã‚‹ã“ã¨ãŒã§ãã¾ã™ã€‚ </p> <source><![CDATA[LocalTransaction tx = AppConfig.getLocalTransaction(); try { tx.begin(TransactionIsolationLevel.SERIALIZABLE); ... tx.commit(); } finally { tx.rollback(); }]]></source> </subsection> <subsection name="セーブãƒã‚¤ãƒ³ãƒˆ"> <p> セーブãƒã‚¤ãƒ³ãƒˆã‚’使用ã™ã‚‹ã“ã¨ã§ã€ãƒˆãƒ©ãƒ³ã‚¶ã‚¯ã‚·ãƒ§ãƒ³ä¸ã®ç‰¹å®šã®å¤‰æ›´ã‚’å–り消ã™ã“ã¨ãŒã§ãã¾ã™ã€‚ </p> <source><![CDATA[LocalTransaction tx = AppConfig.getLocalTransaction(); try { tx.begin(); // 検索ã—ã¦æ›´æ–° Employee employee = dao.selectById(1); employee.setName("hoge"); dao.update(employee); // セーブãƒã‚¤ãƒ³ãƒˆã‚’ä½œæˆ tx.setSavepoint("beforeDelete"); // 削除 dao.delete(employee); // セーブãƒã‚¤ãƒ³ãƒˆã¸æˆ»ã‚‹ï¼ˆä¸Šã§è¡Œã£ãŸå‰Šé™¤ã‚’å–り消ã™ï¼‰ tx.rollback("beforeDelete"); tx.commit(); } finally { tx.rollback(); }]]></source> </subsection> </section> </body> </document>