我可以使用RESOURCE_LOCAL持久性单元提供数据源吗?

时间:2015-03-27 05:29:30

标签: java java-ee jpa

当我有一个具有transaction-type =" RESOURCE_LOCAL"的持久性单元时我可以以某种方式提供数据源(由我的应用程序创建) - 而不是提供javax.persistence.jdbc.url,用户,密码,驱动程序等?

1 个答案:

答案 0 :(得分:0)

是的,你可以。只需创建它并放入应用程序上下文。

在java站点上,在创建EntityManagerFactory之前:

Context initCtx = new InitialContext();

    BasicDataSource bs = new BasicDataSource(); //that is DataSource implementation from apache commons-dbcp
    bs.setDriverClassName("com.mysql.jdbc.Driver");
    ...
    initCtx.bind("MY_DATA_SOURCE", bs);

在你的persistence.xml中

  <persistence-unit name="YourPersistence" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <non-jta-data-source>MY_DATA_SOURCE</non-jta-data-source>
    ....
相关问题