Im setting new database connection with Sql Server using EntityManager(already got EM for mySql database). But during start up app does not distinguish EntityManagers due to some problem with my qualifiers. Can someone tell how to make my EM's distinguishable?
I have added necessary configuration in persistence.xml file. What i have tried so far:
- Created Qualifiers for both mySql and SqlServer units.
- annotated injected fields, constructors, produces methods etc
- even tried to create two different classes for EntityManagerProducer and annotated only them, but it is still not working.
- annotated injected EM in abstract class, which all my DAO's use(got two different abstract classes for each EntityManager Injection).
- Listed all loaded beans - as a result i can see that my Qualifiers are not taken into account Qualifiers:[javax.enterprise.inject.Default,javax.enterprise.inject.Any,javax.inject.Named], EntityManagerProducer...
I got fallowing classes: EntityManagerFactoryProducer and EntityManagerProducer.
@ApplicationScoped
public class EntityManagerFactoryProducer {
private static EntityManagerFactory mySqlEMFactory;
private static EntityManagerFactory sqlServerEMFactory;
public static EntityManagerFactory getEMFactoryMySql() {
if (mySqlEMFactory != null)
return mySqlEMFactory;
return createEMFactoryMySql();
}
public static EntityManagerFactory getEMFactorySqlServer() {
if (sqlServerEMFactory != null)
return sqlServerEMFactory;
return createEMFactorySqlServer();
}
private static synchronized EntityManagerFactory createEMFactoryMySql() {
// To avoid race conditions
if (mySqlEMFactory != null)
return mySqlEMFactory;
String unitName = "mysql";
mySqlEMFactory = Persistence.createEntityManagerFactory(unitName);
return mySqlEMFactory;
}
private static synchronized EntityManagerFactory createEMFactorySqlServer() {
// To avoid race conditions
if (sqlServerEMFactory != null)
return sqlServerEMFactory;
String unitName = "sqlServer";
sqlServerEMFactory = Persistence.createEntityManagerFactory(unitName);
return sqlServerEMFactory;
}
@Produces
@MySql
@ApplicationScoped
public EntityManagerFactory createEntityManagerFactoryMySql() {
return getEMFactoryMySql();
}
@Produces
@SqlServer
@ApplicationScoped
public EntityManagerFactory createEntityManagerFactorySqlServer() {
return getEMFactorySqlServer();
}
public void dispose(@Disposes EntityManagerFactory emFactory) {
if (emFactory.isOpen())
emFactory.close();
}
}
public class EntityManagerProducer {
@Inject
@MySql
private EntityManagerFactory mySqlemFactory;
@Inject
@SqlServer
private EntityManagerFactory sqlServerFactory;
protected EntityManagerProducer() {
}
public EntityManagerProducer(EntityManagerFactory mySqlemFactory, EntityManagerFactory sqlServerFactory) {
this.mySqlemFactory = mySqlemFactory;
this.sqlServerFactory = sqlServerFactory;
}
@Produces
@MySql
@RequestScoped
public EntityManager createEntityManagerForMySql() {
return mySqlemFactory.createEntityManager();
}
@Produces
@SqlServer
@RequestScoped
public EntityManager createEntityManagerForSqlServer() {
return sqlServerFactory.createEntityManager();
}
public void close(@Disposes EntityManager em) {
if (em.isOpen())
em.close();
}
}
//second looks the same
Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE})
public @interface MySql {
}
// And then usage of EM in abstract class for all mySql DAO's
//Sql Server EM is injected the same way in his abstract class for DAO's
public abstract class AbstractBaseReadDao <T extends BaseEntity> {
@Inject @MySql private EntityManager em;
protected AbstractBaseReadDao() {}
protected AbstractBaseReadDao( EntityManager em ) { this.em = em; }
//...
}
After many tries Im always getting the same error
"AM org.apache.webbeans.component.AbstractInjectionTargetBean postConstructDefault SEVERE: An error occurred while executing [@PostConstruct.] javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: Invalid object name 'person'. ... Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'person'"
On the startup im calling service which authorize user using MySql database -table PERSON. So im getting this error because EM with Sql Server configuration is trying to select person from table PERSON(there is no table PERSON in Sql Server database) and it should use EM for MySql. And second think is, that according to where i put annotation, app sometimes is not event creating EM instance for Sql Server.