2017-09-22 79 views
0

當我午餐應用程序時,它不採取這些屬性,而是glassfish-ressource.xml中的默認屬性。我正在使用帶有nebeans自動生成的bean和實體的JPA。我希望在運行時切換數據庫。JPA EntityManagerFactory不覆蓋persistence.xml文件

他在這裏的我的會話類

package facade; 
import entities.Tpe; 
import java.util.HashMap; 
import java.util.Map; 
import javax.annotation.PostConstruct; 
import javax.ejb.Stateless; 
import javax.persistence.EntityManager; 
import javax.persistence.EntityManagerFactory; 
import javax.persistence.Persistence; 
import javax.persistence.PersistenceContext; 

/** 
* 
* @author Emmanuel 
*/ 
@Stateless 
public class TpeFacade extends AbstractFacade<Tpe> { 
    private EntityManager em; 
    @Override 
    public EntityManager getEntityManager() { 

    EntityManagerFactory emf=null; 
    Map properties = new HashMap(); 
    properties.put("javax.persistence.transactionType", "JTA"); 
    properties.put("javax.persistence.jdbc.driver", "jdbc:mysql://192.20.3.81:3306/piv?zeroDateTimeBehavior=convertToNull"); 
    properties.put("javax.persistence.jdbc.url", "com.mysql.jdbc.Driver"); 
    properties.put("javax.persistence.jdbc.database", "piv"); 
    properties.put("javax.persistence.jdbc.user", "username"); 
    properties.put("javax.persistence.jdbc.password", "password"); 

    try { 
     emf = Persistence.createEntityManagerFactory("ConfigurationTPEPU", properties); 
     System.out.println("emfznezizzhzz "+emf.getProperties()); 
    } catch (Exception e) { 
    } 
    em = (EntityManager) emf.createEntityManager(); 
    return em ; 
} 


    public TpeFacade() { 
     super(Tpe.class); 
    } 

} 

和我的持久性文件,內容

<?xml version="1.0" encoding="UTF-8"?> 
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> 
    <persistence-unit name="ConfigurationTPEPU" transaction-type="JTA"> 
    <jta-data-source>java:app/connexion81</jta-data-source> 
    <exclude-unlisted-classes>false</exclude-unlisted-classes> 

    </persistence-unit> 

</persistence> 

了GlassFish,ressources.xml內容

<resources> 
    <jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="mysql_piv_rootPool" non-transactional-connections="false" pool-resize-quantity="2" res-type="javax.sql.DataSource" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false"> 
     <property name="serverName" value="localhost"/> 
     <property name="portNumber" value="3306"/> 
     <property name="databaseName" value="db1"/> 
     <property name="User" value="root"/> 
     <property name="Password" value="gsmcom"/> 
       <property name="URL" value="jdbc:mysql://localhost:3306/petroivoire?zeroDateTimeBehavior=convertToNull"/> 

     <property name="driverClass" value="com.mysql.jdbc.Driver"/> 
    </jdbc-connection-pool> 
    <jdbc-resource enabled="true" jndi-name="java:app/connexion81" object-type="user" pool-name="mysql_piv_rootPool"/> 
</resources> 
+0

如果有人能幫助我,那將是我的榮幸。這三天這個問題阻止了我的進展。對不起,我的英語 –

+0

你到底有什麼問題?你用一些屬性定義了一個持久性單元,但是在創建EMF時將它們取代了它們,所以它在必要時使用覆蓋。就像Javadocs說的那樣 – DN1

+0

這不是一種好的Java EE風格,因爲您不會使用連接池,而無狀態EJB將爲每個池實例創建一個EntityManagerFactory。爲什麼要切換數據庫而不使用數據源? –

回答

0

你應該理解的是,對於@EJB應用程序服務器在這裏t關心@EJB的吸引力,而且注重持久性。

使用的配置是Glassfish域(domain/config/domain.xml)中已有的配置,而不是glassfish-ressource.xml文件的這些屬性。

這意味着,覆蓋你已經第一個配置使用兩個不放過和:編程它們之間

  1. 開關(二者同時注射時)。

  2. 或使用Persistence.createEntityManagerFactory(jndiDataSource2NdUnitName).createEntityManager();其中jndiDataSource2NdUnitName是第二個單元名稱。

+0

非常感謝你領導,我很瞭解它,但我有一個問題,我想添加一個新的數據庫與redoploy我的應用程序。因爲使用了您添加新數據庫的方法,通過添加新的數據庫配置來強制修改persistence.xml。沒有其他方法以編程方式添加新的數據庫。謝謝 –

+0

根據規範,不可能以動態方式添加持久性單元。但如果你不使用APP服務器,對不起,我不知道。 – bilelovitch

+0

非常感謝你,我會嘗試不同的方式 –