2012-06-25 40 views
0

我在當前的Web項目中使用JP2。我的主數據庫包含主要實體。JPA2運行時數據庫連接

Persistance.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> 
<persistence-unit name="MyPU" transaction-type="JTA"> 

    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> 

    <jta-data-source>MyDB</jta-data-source> 

    <exclude-unlisted-classes>false</exclude-unlisted-classes> 

    <properties> 
     <property name="eclipselink.logging.level" value="FINE"/> 
     <property name="eclipselink.logging.parameters" value="true"/> 
     <property name="eclipselink.logging.logger" value="ServerLogger"/> 
     <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/> 
    </properties> 
</persistence-unit> 
</persistence> 

和JTA數據源在陽光resources.xml中定義的:要在此DB我定義了一個Persitence單位用JTA數據源連接

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd"> 
<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="org.postgresql.ds.PGSimpleDataSource" 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="post-gre-sql_mydb_mypool" 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="5432"/> 
     <property name="databaseName" value="mydb"/> 
     <property name="User" value="myuser"/> 
     <property name="Password" value="mypass"/> 
     <property name="URL" value="jdbc:postgresql://localhost:5432/mydb"/> 
     <property name="driverClass" value="org.postgresql.Driver"/> 
     <property name="characterEncoding" value="UTF-8" /> 
    </jdbc-connection-pool> 
    <jdbc-resource enabled="true" jndi-name="MyDB" object-type="user" pool-name="post-gre-sql_mydb_mypoll"/> 
</resources> 

而且這是我如何訪問我的DAO類的數據庫(女巫是@ManagedBeans和@SessionScoped):

@ManagedBean(name = "pageDao") 
@SessionScoped 
public class PageDao implements Serializable { 

    @Resource 
    private UserTransaction utx = null; 

    @PersistenceUnit(unitName = "MyPU") 
    private EntityManagerFactory emf = null; 

    public EntityManager getEntityManager() { 
     return emf.createEntityManager(); 
    } 

    public List<PageEnt> getAll() { ... } 

    public PageEnt getOne(long pageId) { ... } 

    public void addPage(PageEnt newPage) throws RollbackFailureException, PreexistingEntityException, Exception { ... } 

    public PageEnt update(PageEnt page) throws RollbackFailureException, NonexistentEntityException, Exception { ... } 

    public void remove(PageEnt page) throws RollbackFailureException, Exception { ... } 
} 

其中一個實體(客戶)具有連接到單獨(每個客戶)數據庫的屬性,女巫在運行時定義。這些特性包括:

  • DATABSE名
  • 主機和端口
  • 用戶和密碼

我的問題是:

  • 如何有效地創建在運行一個數據庫連接-時間?
  • 如果沒有定義每個客戶的PersistanceUnit和Datasources(女巫是在部署時定義的),如何從容器管理的資源創建新的EntityManager?
  • 如果我必須手動處理EntityManagerFactory(女巫,就像我在大學學到的那樣,是一個沉重而膨脹的對象),我該如何有效地做到這一點?有沒有好的做法或模式?
  • DAO模式如何工作?我的DAO類如何獲得EntityManager?

非常感謝巴西。

回答

0

運行時可以在多個數據源之間切換。它由Spring AbstractRoutingDataSource提供。需要重寫#determineCurrentLookupKey()方法,該方法將返回一個鍵來決定需要連接的特定數據源。還應該有彈簧配置,它映射每個可能的鍵和要連接的相應數據源。像
<jee:jndi-lookup id="DataSource_Client1" jndi-name="DataSource_Client1" /> <jee:jndi-lookup id="DataSource_Client2" jndi-name="DataSource_Client" />
<bean id="DynamicDataSource" class="concrete implementation class name of AbstractRoutingDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry key="Client1" value-ref="DataSource_Client1" />
<entry key="Client2" value-ref="DataSource_Client2" />
</map>
</property>
</bean>

有些事情可以參考這個Dynamic DataSource

希望這回答了你的問題之一