我想将两个连接参数化为DataBase。
我创建了两个生产者:
@Singleton
public class DataBaseProducerDB2 {
@Produces
@PersistenceContext(unitName = "contratbusiness")
private EntityManager em;
public EntityManager getEm() {return em;}
}
@Singleton
public class DataBaseProducerMySQL {
public EntityManager getEm() {
return em;
}
@Produces
@PersistenceContext(unitName = "AutreSQL")
private EntityManager em;
}
然后,我注入了我测试所需的制作人,
@Stateless(name = "contratbusiness.net.awl.bfi.contratbusiness.queries.DacomccbQueries")
@TransactionAttribute(TransactionAttributeType.SUPPORTS)
@LocalBean
@Default
public class DacomccbQueries {
@Inject
private DataBaseProducerMySQL baseProducerMySQL;
public List<Object[]> findInfosContratOuvert(final String contrat) {
final TypedQuery<Object[]> query = this.baseProducerMySQL.getEm().createNamedQuery(
"net.awl.bfi.contratbusiness.entities.Dacomccb.findInfosContratOuvert", Object[].class);
query.setParameter("contrat", contrat);
return query.getResultList();
}
我不明白为什么当我与SQL生产者一起启动Junit Test时,会启动DB2生产者。
@RunWith(EJBContainerRunner.class)
public class DacomccbQueriesTest {
@Inject
private DacomccbQueries dacomccbQueries;
}
这里是日志:
(SchemaExport.java:perform:426) - [DataDirect] [DB2 JDBC Driver] [DB2] ......
更新:添加persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence 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"
version="2.0">
<persistence-unit name="contratbusiness" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/contratbusiness.DB</jta-data-source>
<class>net.awl.bfi.contratbusiness.entities.Dacomccb</class>
<class>net.awl.bfi.contratbusiness.entities.Daccbcpt</class>
<class>net.awl.bfi.contratbusiness.entities.Dacpt</class>
<class>net.awl.bfi.contratbusiness.entities.Daccbadr</class>
<class>net.awl.bfi.contratbusiness.entities.Daccbdvs</class>
<class>net.awl.bfi.contratbusiness.entities.Dacompdv</class>
<class>net.awl.bfi.contratbusiness.entities.keyid.DaccbcptId</class>
<class>net.awl.bfi.contratbusiness.entities.keyid.DacomccbId</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
<validation-mode>CALLBACK</validation-mode>
<properties>
<property name="hibernate.ejb.cfgfile" value="hibernate.cfg.xml" />
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider" />
</properties>
</persistence-unit>
<persistence-unit name="AutreSQL" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/contratbusiness.DB</jta-data-source>
<class>net.awl.bfi.contratbusiness.entities.Dacomccb</class>
<class>net.awl.bfi.contratbusiness.entities.Daccbcpt</class>
<class>net.awl.bfi.contratbusiness.entities.Dacpt</class>
<class>net.awl.bfi.contratbusiness.entities.Daccbadr</class>
<class>net.awl.bfi.contratbusiness.entities.Daccbdvs</class>
<class>net.awl.bfi.contratbusiness.entities.Dacompdv</class>
<class>net.awl.bfi.contratbusiness.entities.keyid.DaccbcptId</class>
<class>net.awl.bfi.contratbusiness.entities.keyid.DacomccbId</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
<validation-mode>CALLBACK</validation-mode>
<properties>
<property name="hibernate.ejb.cfgfile" value="hibernate.cfg.mysql.xml" />
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider" />
</properties>
</persistence-unit>
</persistence>
答案 0 :(得分:0)
两个持久性单元都使用相同的数据源,因此当数据源被修改时,它可以工作。