Play2.xとDBfluteの連携について

572 views
Skip to first unread message

Tomoaki Koseki

unread,
Jan 8, 2014, 2:16:23 AM1/8/14
to pla...@googlegroups.com
はじめまして、小関と申します。
最近PlayFrameworkを触り始めた者です。

PlayFramework2.x系からDBFluteを使用している方が居たら教えて頂きたく。
DIはGuiceを使用しようとしていますが、トランザクション管理がうまく操作出来なく困っています。
DB接続は行えて、SelectやUpdate等は出来ていますが、例外を発生させても、Rollbackされません。
JPAクラス経由で明示的にRollbackを指示しても、Rollbackされませんでした。

以下を参考に作成させて頂いていますが、PlayFrameworkを2.x系にしたいと思っています。

xeno...@gmail.com

unread,
Jan 13, 2014, 8:13:45 PM1/13/14
to pla...@googlegroups.com
はじめまして
阿部と申します

PlayFramework2.2.xでDBFluteを使用しています

※とりあえずざらっと(あとでどこかでまとめたいと思っています...)

トランザクション処理はSpringを使用しています

build.sbt (抜粋)
libraryDependencies ++= Seq(
  javaJdbc,
  "com.google.inject" % "guice" % "3.0",
  "org.seasar.dbflute" % "dbflute-runtime" % "1.0.5A",
  "org.springframework" % "spring-jdbc" % "3.2.4.RELEASE",
  "org.springframework" % "spring-aop" % "3.2.4.RELEASE"
)
 
resolvers += "The Seasar Foundation Maven2 Repository" at "http://maven.seasar.org/maven2"

Global.java (抜粋)
    public void onStart(Application app) {
        super.onStart(app);
        List<Module> moduleList = new ArrayList<Module>();
        DataSource dataSource = DB.getDataSource();
        moduleList.add(new TransactionModule(dataSource));
        moduleList.add(new DBFluteModule(new TransactionAwareDataSourceProxy(dataSource)));
        Injector injector;
        if (app.isProd())
            injector = Guice.createInjector(Stage.PRODUCTION, moduleList.toArray(new Module[] {}));
        else
            injector = Guice.createInjector(Stage.DEVELOPMENT, moduleList.toArray(new Module[] {}));
    }

    protected static class TransactionModule extends AbstractModule {
        protected DataSource dataSource;
        public TransactionModule(DataSource dataSource) {
            if (dataSource == null) {
                String msg = "The argument 'dataSource' should not be null!";
                throw new IllegalArgumentException(msg);
            }
            this.dataSource = dataSource;
        }
        @Override
        protected void configure() {
            // TransactionManager
            PlatformTransactionManager transactionManager = new DataSourceTransactionManager(dataSource);
            // TransactionInterceptor
            Properties properties = new Properties();
            properties.setProperty("*", "PROPAGATION_REQUIRED");
            TransactionInterceptor transactionInterceptor = new TransactionInterceptor(transactionManager, properties);
            bindInterceptor(Matchers.any(), Matchers.annotatedWith(Transactional.class), transactionInterceptor);
            bind(PlatformTransactionManager.class).toInstance(transactionManager);
            //bind(DataSource.class).toInstance(dataSource);
        }
    }
 
これで、あとはコントローラー等で Transactional アノテーションをつければトランザクション処理されます
    @Transactional
    public Result test() {
        fooBhv.update(entity);
        return ok(result);
    }

それと、conf/application-logger.xml に以下のような記述をしておくと開発しやすいかと思います
application-logger.xml (抜粋)
  <logger name="com.jolbox.bonecp" level="TRACE" />
  <logger name="org.springframework" level="DEBUG" />
  <logger name="org.seasar.dbflute" level="INFO" />
  <logger name="org.seasar.dbflute.QLog" level="DEBUG" />

Tomoaki Koseki

unread,
Jan 14, 2014, 2:06:32 AM1/14/14
to pla...@googlegroups.com
阿部さん、ご回答ありがとうございます。

大変参考になりました。
また何か有りましたら、よろしくお願いします。

ありがとうございます。

2014年1月14日火曜日 10時13分45秒 UTC+9 Xenon Abe:
Reply all
Reply to author
Forward
0 new messages