Connection to two different databases in Spring JPA fails -
i trying use 2 different data sources/databases in application far not able connect them.
firstdb config file
@configuration @enabletransactionmanagement @enablejparepositories(basepackages = "xx.xx.xx.crud.repository.running", entitymanagerfactoryref = "nextgenentitymanagerfactory", transactionmanagerref = "transactionmanagerone") @propertysource("classpath:application.properties") public class nextgendbconfig { @value("${spring.datasourcenextgenlims.driver-class-name}") string driverclassname = ""; @value("${spring.datasourcenextgenlims.url}") string url = ""; @value("${spring.datasourcenextgenlims.username}") string username = ""; @value("${spring.datasourcenextgenlims.password}") string password = ""; @autowired @qualifier("jpanextgenvendorapapter") jpavendoradapter jpanextgenvendorapapter; @bean(name = "nextgendatasource") @primary public datasource nextgendatasource() { return datasourcebuilder.create().url(url) .driverclassname(driverclassname).username(username) .password(password).build(); } @bean @primary public localcontainerentitymanagerfactorybean nextgenentitymanagerfactory() { localcontainerentitymanagerfactorybean factorybean = new localcontainerentitymanagerfactorybean(); factorybean.setdatasource(nextgendatasource()); factorybean.setjpavendoradapter(jpanextgenvendorapapter); factorybean.setpackagestoscan(r.nextgendb_package); factorybean.setpersistenceunitname("nextgenpersistenceunit"); factorybean.afterpropertiesset(); return factorybean; } @bean platformtransactionmanager transactionmanagerone() { return new jpatransactionmanager(nextgenentitymanagerfactory() .getobject()); } @bean(name = "jpanextgenvendorapapter") @primary public jpavendoradapter jpanextgenvendoradapter() { hibernatejpavendoradapter jpanextgenvendoradapter = new hibernatejpavendoradapter(); jpanextgenvendoradapter.setshowsql(true); jpanextgenvendoradapter.setgenerateddl(true); jpanextgenvendoradapter.setdatabase(database.mysql); return jpanextgenvendoradapter; } @bean public hibernateexceptiontranslator hibernateexceptiontranslator() { return new hibernateexceptiontranslator(); } }
second db config
@configuration @enabletransactionmanagement @enablejparepositories(basepackages = "xx.xx.xx.xx.xx.running", entitymanagerfactoryref = "gatclimsentitymanagerfactory", transactionmanagerref = "transactionmanagertwo") @propertysource("classpath:application.properties") public class gatcdbconfig { @value("${spring.datasourcegatclims.driver-class-name}") string driverclassname = ""; @value("${spring.datasourcegatclims.url}") string url = ""; @value("${spring.datasourcegatclims.username}") string username = ""; @value("${spring.datasourcegatclims.password}") string password = ""; @autowired @qualifier("jpamygatcvendoradapter") jpavendoradapter mygatcvendoradapter; @bean(name = "gatclimsdatasource") public datasource gatclimsdatasource() { return datasourcebuilder.create().url(url) .driverclassname(driverclassname).username(username) .password(password).build(); } @bean public localcontainerentitymanagerfactorybean gatclimsentitymanagerfactory() { localcontainerentitymanagerfactorybean factorybean = new localcontainerentitymanagerfactorybean(); factorybean.setdatasource(gatclimsdatasource()); factorybean.setjpavendoradapter(mygatcvendoradapter); factorybean.setpackagestoscan(r.gatcdb_package); factorybean.setpersistenceunitname("gatclimspersistenceunit"); factorybean.afterpropertiesset(); return factorybean; } @bean platformtransactionmanager transactionmanagertwo() { return new jpatransactionmanager(gatclimsentitymanagerfactory() .getobject()); } @bean(name = "jpamygatcvendoradapter") public jpavendoradapter jpamygatcvendoradapter() { hibernatejpavendoradapter jpavendoradapter = new hibernatejpavendoradapter(); jpavendoradapter.setshowsql(true); jpavendoradapter.setgenerateddl(true); jpavendoradapter.setdatabase(database.mysql); return jpavendoradapter; } }
when start server, recieve exception
2015-08-19 15:21:25.412 error 16928 --- [ main] org.hibernate.tool.hbm2ddl.schemaupdate : hhh000299: not complete schema update com.mysql.jdbc.exceptions.jdbc4.mysqlsyntaxerrorexception: access denied user 'xyz'@'192.168.xx.xx' database 'xyz' @ sun.reflect.nativeconstructoraccessorimpl.newinstance0(native method) @ sun.reflect.nativeconstructoraccessorimpl.newinstance(nativeconstructoraccessorimpl.java:62) @ sun.reflect.delegatingconstructoraccessorimpl.newinstance(delegatingconstructoraccessorimpl.java:45) @ java.lang.reflect.constructor.newinstance(constructor.java:422) @ com.mysql.jdbc.util.handlenewinstance(util.java:377) @ com.mysql.jdbc.util.getinstance(util.java:360) @ com.mysql.jdbc.sqlerror.createsqlexception(sqlerror.java:978) @ com.mysql.jdbc.mysqlio.checkerrorpacket(mysqlio.java:3887) @ com.mysql.jdbc.mysqlio.checkerrorpacket(mysqlio.java:3823) @ com.mysql.jdbc.mysqlio.checkerrorpacket(mysqlio.java:870) @ com.mysql.jdbc.mysqlio.proceedhandshakewithpluggableauthentication(mysqlio.java:1659) @ com.mysql.jdbc.mysqlio.dohandshake(mysqlio.java:1206) @ com.mysql.jdbc.connectionimpl.coreconnect(connectionimpl.java:2234) @ com.mysql.jdbc.connectionimpl.connectonetryonly(connectionimpl.java:2265) @ com.mysql.jdbc.connectionimpl.createnewio(connectionimpl.java:2064) @ com.mysql.jdbc.connectionimpl.<init>(connectionimpl.java:790) @ com.mysql.jdbc.jdbc4connection.<init>(jdbc4connection.java:44)
and 1 in same exception (though little later)
caused by: org.springframework.beans.factory.nouniquebeandefinitionexception: no qualifying bean of type [javax.persistence.entitymanagerfactory] defined: expected single matching bean found 2: gatclimsentitymanagerfactory,nextgenentitymanagerfactory @ org.springframework.orm.jpa.support.persistenceannotationbeanpostprocessor.finddefaultentitymanagerfactory(persistenceannotationbeanpostprocessor.java:572) @ org.springframework.orm.jpa.support.persistenceannotationbeanpostprocessor.findentitymanagerfactory(persistenceannotationbeanpostprocessor.java:531) @ org.springframework.orm.jpa.support.persistenceannotationbeanpostprocessor$persistenceelement.resolveentitymanager(persistenceannotationbeanpostprocessor.java:697) @ org.springframework.orm.jpa.support.persistenceannotationbeanpostprocessor$persistenceelement.getresourcetoinject(persistenceannotationbeanpostprocessor.java:670) @ org.springframework.beans.factory.annotation.injectionmetadata$injectedelement.inject(injectionmetadata.java:169) @ org.springframework.beans.factory.annotation.injectionmetadata.inject(injectionmetadata.java:88) @ org.springframework.orm.jpa.support.persistenceannotationbeanpostprocessor.postprocesspropertyvalues(persistenceannotationbeanpostprocessor.java:354) ... 29 more
my properties files read connection details
spring.datasourcenextgenlims.driver-class-name=com.mysql.jdbc.driver spring.datasourcenextgenlims.url=jdbc:mysql://xx.xx.xx.xx/xyz spring.datasourcenextgenlims.username=xxx spring.datasourcenextgenlims.password=xxx spring.datasourcegatclims.driver-class-name=com.mysql.jdbc.driver spring.datasourcegatclims.url=jdbc:mysql://xx.xx.xx.xx/abc spring.datasourcegatclims.username=xxx spring.datasourcegatclims.password=xxx few observations:
in exception can seen trying connect local db ,192.168.xx.xx local address.
com.mysql.jdbc.exceptions.jdbc4.mysqlsyntaxerrorexception: access denied user 'xyz'@'192.168.xx.xx' database 'xyz'
(which not exist) ,i trying connect 2 different remote db's defined in properties file.why goes local?
when comment 1 of config other runs fine (though no access problem comes) problem of
no qualifying bean of type [javax.persistence.entitymanagerfactory]
have tried make 1 of bean primary,tried making simple function of no use.
please me in solving problem . thanks.
you can avoid doing this
@value("${spring.datasourcegatclims.driver-class-name}") string driverclassname = ""; @value("${spring.datasourcegatclims.url}") string url = ""; @value("${spring.datasourcegatclims.username}") string username = ""; @value("${spring.datasourcegatclims.password}") string password = ""; @autowired @qualifier("jpamygatcvendoradapter") jpavendoradapter mygatcvendoradapter; @bean(name = "gatclimsdatasource") public datasource gatclimsdatasource() { return datasourcebuilder.create().url(url) .driverclassname(driverclassname).username(username) .password(password).build(); }
and instead reduce code lines below:
@configurationproperties(prefix = "spring.datasourcegatclims") @bean @primary public datasource postgresdatasource() { return datasourcebuilder.create(). build(); }
in order avoid exception below can name bean entitymanagerfactory
caused by: org.springframework.beans.factory.nouniquebeandefinitionexception: no qualifying bean of type [javax.persistence.entitymanagerfactory] defined: expected single matching bean found 2: gatclimsentitymanagerfactory,nextgenentitymanagerfactory
@bean(name = "entitymanagerfactory") @primary public localcontainerentitymanagerfactorybean emf1(entitymanagerfactorybuilder builder){ return builder .datasource(postgresdatasource()) .packages("io.eddumelendez.springdatajpa.domain1") .persistenceunit("users") .build(); }
checkout complete configuration here
Comments
Post a Comment