0

This is the new file I've added in addition to the existing Persistenceconfig.Java. I'm getting this entity manager as 2 found.

  1. we're not using any xml config except for the jpa repositories in spring-data.xml

  2. The issues is occuring only for one package created newly for logging activity and that is also included in jpa repository.

Before adding the below class, everything is normal previously

package com.jumbotree.kumcha.config;

import java.util.Properties;

import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.dao.support.PersistenceExceptionTranslator;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
//import org.springframework.orm.hibernate5.HibernateExceptionTranslator;
import org.springframework.orm.hibernate4.HibernateExceptionTranslator;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
//@PropertySource("classpath:kumcha.properties")
@PropertySource("file:/opt/jumbotree/kumcha2/kms.properties")
@EnableJpaRepositories(basePackages = "com.jumbotree.kumcha.crm.model", entityManagerFactoryRef = "createEntityManagerFactoryChargingBean", transactionManagerRef = "createChargingTransactionManagerBean")
@ImportResource("classpath:spring-data.xml")
public class ChargingPersistenceConfig {
    @Autowired
    private Environment env;

    private static final Logger LOGGER = LoggerFactory.getLogger(ChargingPersistenceConfig.class);


    @Bean  
    public DataSource createChargingDataSourceBean() {
        LOGGER.info("Charging Datasource created...");
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(env.getProperty("charging.db.driver"));
        dataSource.setUrl(env.getProperty("charging.db.url"));
        dataSource.setUsername(env.getProperty("charging.db.username"));
        dataSource.setPassword(env.getProperty("charging.db.password"));
        return dataSource;
    }

    //Here is the entity manager added and causing this issue
    @Bean   //(name = "entityManagerFactoryCharging")
    @PersistenceContext (unitName="chargingPU")
    public FactoryBean<EntityManagerFactory> createChargingEntityManagerFactoryBean(@Qualifier("createChargingDataSourceBean") DataSource dsc) {
        LocalContainerEntityManagerFactoryBean chargingentityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        LOGGER.info("Charging entityman created...");
        try {
            chargingentityManagerFactoryBean.setDataSource(createChargingDataSourceBean());
            chargingentityManagerFactoryBean.setPackagesToScan("com.jumbotree.kumcha.crm.model");
            chargingentityManagerFactoryBean.setJpaVendorAdapter(createJpaVendorAdapterBean());
            chargingentityManagerFactoryBean.setJpaProperties(createJpaProperties());
        } catch (Exception e) {
            // TODO: handle exception
            LOGGER.error(e.toString());
        }


        return chargingentityManagerFactoryBean;
    }

    private Properties createJpaProperties() {
        LOGGER.info("hibernate.show_sql :::: "+env.getProperty("hibernate.show_sql"));
        return new Properties() {
            {
//                setProperty("hibernate.hbm2ddl.auto", "create-drop");
                setProperty("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
                setProperty("hibernate.format_sql", env.getProperty("hibernate.format_sql"));
               // setProperty("hibernate.cache.use_second_level_cache", "true");
               // setProperty("hibernate.cache.provider_class", "org.hibernate.cache.EhCacheProvider");
               // setProperty("shared-cache-mode", "DISABLE_SELECTIVE");
                //<property name="hibernate.cache.use_second_level_cache" value="true"/>
                //setProperty("hibernate.connection.zeroDateTimeBehavior", "convertToNull");
            }
        };
    }

    private JpaVendorAdapter createJpaVendorAdapterBean() {
        HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
//        jpaVendorAdapter.setDatabase(Database.valueOf(env.getProperty("db.name")));
        jpaVendorAdapter.setShowSql(true);
//        jpaVendorAdapter.setGenerateDdl(true);
        jpaVendorAdapter.setDatabasePlatform(env.getProperty("hibernate.dialect"));
        return jpaVendorAdapter;
    }

    @Bean   //(name = "transactionManager")
    public PlatformTransactionManager createChargingTransactionManagerBean() throws Exception {
        LOGGER.info("Charging transactionMan created...");
        JpaTransactionManager chargingtransactionManager = new JpaTransactionManager();
        chargingtransactionManager.setEntityManagerFactory(createChargingEntityManagerFactoryBean(createChargingDataSourceBean()).getObject());
        return chargingtransactionManager;
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor createPersistenceExceptionTranslationPostProcessor() {
        return new PersistenceExceptionTranslationPostProcessor();
    }

    // Required if using Hibernate 4
    @Bean
    public PersistenceExceptionTranslator createPersistenceExceptionTranslatorBeaan() {
        return new HibernateExceptionTranslator();
    }

}
SatyaRajC
  • 51
  • 7
  • Possibly related: https://stackoverflow.com/q/8414287/1531971 –  Aug 18 '17 at 14:34
  • Could you please post a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve)? – Sergey Vyacheslavovich Brunov Aug 18 '17 at 23:31
  • Hi All, I've added the complete class of new persistenceconfig I tried to create for new DB connection. Now there are two persistenceconfig files each datasource trying to create connection with db's on different IP's – SatyaRajC Aug 20 '17 at 11:21

1 Answers1

3

LoggerRepository cannot choose one of the 2 beans createChargingEntityManagerFactoryBean and createEntityManagerFactoryBean

Make one of them primary and/or specify qualifier. (BTW sometimes even with qualifier it's necessary to make one of beans primary)

Cannot suggest cconfig changes without your code.

StanislavL
  • 56,971
  • 9
  • 68
  • 98