2014-11-21 84 views
1

我使用的GlassFish 4和Java EE 7,我需要定義一個連接池是爲每個應用程序,我將在GlassFish中部署不同的定義不同的連接池。如何通過部署的應用程序在GlassFish中

我有每個客戶端一個應用程序(.war文件),並且每個客戶都有自己的用戶名/密碼/模式在我的MySQL數據庫,這樣數據就不是客戶端之間共享。 我知道如何在glassfish中定義連接池,但是我的所有應用程序只能使用相同的設置(我正在使用bonecp btw)。 我希望能夠爲每個部署的應用程序更改用戶/密碼/架構。是否有可能在persistence.xml中完全定義連接池而不是在glassfish中,所以我可以在不同的.war文件中有不同的連接池? 隨着10個.war文件的部署(10個客戶端),我想有10個不同的連接池(不同的用戶/密碼/架構定義)。

回答

0

如果以編程方式創建數據源,那麼您可以將其注入到JPA沒有必要宣佈它在你的persistence.xml。下面是一個例子

定義持久性的XML:

<persistence-unit name="foo-PU" transaction-type="RESOURCE_LOCAL"> 
    <!-- the provider: Hibernate, EclipseLink or another --> 
    <provider>org.hibernate.ejb.HibernatePersistence</provider> 

    <!-- No need to define any connection parameter nor the data source --> 

    <!-- class definitions here --> 
</persistence-unit> 

定義屬性文件來存儲數據源的配置:

db.driver=com.mysql.jdbc.Driver 
db.url=jdbc:mysql://localhost:3306/foo_db 
db.user=user 
db.password=s3cr3t 
db.bonecp.idleConnectionTestPeriod=60 
db.bonecp.idleMaxAge=240 
db.bonecp.maxConnections=10 
# more properties... 

定義類,將生成的數據源

public class DataSourceGenerator { 
    public static DataSource getDataSource(String properties) { 
     Properties conf = new Properties(); 
     try { 
      conf.load(
       DataSourceFactory.class 
       .getClassLoader().getResourceAsStream(
        properties)); 
     } catch (IOException e) { 
      //handle the error 
      //naive handling shown here 
      e.printStacktrace(); 
     } 
     BoneCPDataSource dataSource = new BoneCPDataSource(); 
     //set the properties from the .properties file 
     dataSource.setDriverClass(conf.getProperty("db.driver")); 
     dataSource.setJdbcUrl(conf.getProperty("db.url")); 
     dataSource.setUsername(conf.getProperty("db.user")); 
     dataSource.setPassword(conf.getProperty("db.password")); 
     dataSource.setIdleConnectionTestPeriodInMinutes(
      Long.parseLong(
       conf.getProperty("db.bonecp.idleConnectionTestPeriod"))); 
     dataSource.setIdleMaxAgeInSeconds(
      Long.parseLong(
       conf.getProperty("db.bonecp.idleMaxAge"))); 
     dataSource.setMaxConnectionsPerPartition(
      Integer.parseInt(
       conf.getProperty("db.bonecp.maxConnections"))); 
     //more properties to load... 
     return dataSource; 
    } 
} 

以編程方式創建您的EntityManagerFactory以及:

public class EntityManagerFactoryGenerator { 
    public static EntityManagerFactory createEMF() { 
     Map<String, Object> properties = new HashMap<>(); 
     String dataSourceKey = ""; 
     //uncomment here depending on your needs... 
     //using Hibernate 
     //dataSourceKey = org.hibernate.cfg.AvailableSettings.DATASOURCE; 
     //using EclipseLink 
     //dataSourceKey = org.eclipse.persistence 
     // .config.PersistenceUnitProperties.NON_JTA_DATASOURCE; 
     properties.put(
      dataSourceKey, 
      DataSourceGenerator.getDataSource("mysql-con.properties")); 
     return Persistence.createEntityManagerFactory("foo-PU", properties); 
    } 
} 
0

第1步。轉到Glassfish管理控制檯來配置JDBC連接詳細信息。

內部資源 - JDBC爲每一個組連接一個細節池,然後創建一個JDBC數據源到每個創建的池。

步驟2.進入每個應用程序,並指向正確的數據源的持久化文件。

不要指定供應商,如果你不需要任何特殊的Glassfish附帶一個的EclipseLink已經,工作正常。

相關問題