2009-11-04 46 views
3

我有一個完美工作的應用程序客戶端,部署到GlassFish v2服務器中,並與某些EJB,實體等一起使用。我正在使用eclipselink。Java EE中的Persistence.createEntityManagerFactory()忽略JTA源

目前我有我的persistence.xml:

<persistence-unit name="mysource"> 
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> 
    <jta-data-source>jdbc/mysource</jta-data-source> 
    <class>entities.one</class> 
    <class>entities.two</class> 
    ... 
    <properties> 
     <property name="eclipselink.target-server" value="SunAS9"/> 
     <property name="eclipselink.logging.level" value="FINE"/> 
    </properties> 
</persistence-unit> 

當我注入EntityManager到EJB能正常工作:

@PersistenceContext(unitName="mysource") 
private EntityManager em; 

現在我有一個要求,動態切換持久化單元/數據庫。 我想我可以得到一個EntityManager編程:

em = Persistence.createEntityManagerFactory("mysource").createEntityManager(); 

,但我得到了以下錯誤:

Unable to acquire a connection from driver [null], user [null] and URL [null] 

即使在Map 「壓倒一切」 javax.persistence.jtaDataSource」到 「JDBC/MYSOURCE」並呼籲createEntityManagerFactory("mysource", map)不會有所作爲。

我缺少什麼?

+0

這應該工作,你的persistence.xml看起來是正確的。檢查你的類路徑中沒有多個persistence.xml。 – James 2013-04-08 13:46:00

回答

1

您嘗試以編程方式繞過容器創建實體管理器,這意味着您很可能會創建非JTA數據源(因爲它位於容器外部,事務類型應該爲RESOURCE_LOCAL),因此您的原始配置是無用的。

嘗試使用不同的unitName屬性注入實體管理器或創建RESOURCE_LOCAL事務類型持久性單元。

+0

我正在嘗試*做的是註釋所做的事情,但是在代碼中,我可以通過編程方式設置unitName。我基本上只是想問一個實體經理的容器名稱給定的PU。如果還有其他方法可以做到這一點(即沒有createEntityManagerFactory),那很好。 – Draemon 2009-11-13 16:43:53