How to use multiple datasources in the same class? - java

How do I make all the datasources configured in bean.xml available in my controller Class1.
bean.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd ">
<bean id="xxxbc"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="username" value="xxxbc" />
<property name="url" value="jdbc:mysql://localhost:3306/xxxbc" />
<property name="password" value="xxxbc" />
</bean>
<bean id="xxxbb"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="username" value="xxxbb" />
<property name="url" value="jdbc:mysql://localhost:3306/xxxbb" />
<property name="password" value="xxxbb" />
</bean>
<bean id="yyybc"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="username" value="yyybc" />
<property name="url" value="jdbc:mysql://localhost:3306/yyybc" />
<property name="password" value="yyybc" />
</bean>
<bean id="yyybb"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="username" value="yyybb" />
<property name="url" value="jdbc:mysql://localhost:3306/yyybb" />
<property name="password" value="yyybb" />
</bean>
<bean id="xxxcctemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="xxxcc"></property>
</bean>
<bean id="xxxbbtemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="xxxbb"></property>
</bean>
<bean id="yyybctemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="yyybc"></property>
</bean>
<bean id="yyybbtemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="yyybb"></property>
</bean>
<bean id="class1" class="com.controller.Class1">
<property name="jdbcTemplate" ref="xxxcctemplate"></property>
</bean>
<bean id="class2" class="com.controller.Class1">
<property name="jdbcTemplate" ref="xxxbbtemplate"></property>
</bean>
<bean id="class3" class="com.controller.Class1">
<property name="jdbcTemplate" ref="yyybctemplate"></property>
</bean>
<bean id="class4" class="com.controller.Class1">
<property name="jdbcTemplate" ref="yyybbtemplate"></property>
</bean>
</beans>

Related

Cannot create JDBC driver of class 'org.postgresql.Driver' for connect URL postgres://username:password#localhost:5432/dbname

I have deployed two Spring roo application on Tomcat 7 server both have database connection while hitting any service of application I am getting below exception
Cannot create JDBC driver of class 'org.postgresql.Driver' for connect URL 'postgres://username:password#localhost:5432/db'
java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(DriverManager.java:278)
at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1437)
at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:139)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcServicesImpl.java:279)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:124)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
Please find below database base configuration
Application 1
application-Context.xml
<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
<property name="driverClassName" value="${database.driverClassName}"/>
<property name="url" value="${database.url}"/>
<property name="username" value="${database.username}"/>
<property name="password" value="${database.password}"/>
<property name="testOnBorrow" value="true"/>
<property name="testOnReturn" value="true"/>
<property name="testWhileIdle" value="true"/>
<property name="timeBetweenEvictionRunsMillis" value="1800000"/>
<property name="numTestsPerEvictionRun" value="3"/>
<property name="minEvictableIdleTimeMillis" value="1800000"/>
<property name="validationQuery" value="SELECT version();"/>
</bean>
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
<property name="persistenceUnitName" value="persistenceUnit"/>
<property name="dataSource" ref="dataSource"/>
</bean>
Application 1. Database.properties
database.driverClassName=org.postgresql.Driver
database.url=jdbc\:postgresql\://localhost\:5432/db
database.username=username
database.password=password
Application 2
application-Context.xml
<bean class="java.net.URI" id="dbUrl">
<constructor-arg value="postgres://username:password#localhost:5432/db"/>
</bean>
<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="#{ 'jdbc:postgresql://' + #dbUrl.getHost() + ':' + #dbUrl.getPort() + #dbUrl.getPath() }"/>
<property name="username" value="#{ #dbUrl.getUserInfo().split(':')[0] }"/>
<property name="password" value="#{ #dbUrl.getUserInfo().split(':')[1] }"/>
<property name="testOnBorrow" value="true"/>
<property name="testOnReturn" value="true"/>
<property name="testWhileIdle" value="true"/>
<property name="timeBetweenEvictionRunsMillis" value="1800000"/>
<property name="numTestsPerEvictionRun" value="3"/>
<property name="minEvictableIdleTimeMillis" value="1800000"/>
<property name="validationQuery" value="SELECT version();"/>
</bean>
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
<util:properties id="regex.properties" location="classpath:META-INF/spring/regex.properties"/>
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
<property name="persistenceUnitName" value="persistenceUnit"/>
<property name="dataSource" ref="dataSource"/>
</bean>
Please advice how can I resolve my Problem..
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="org.postgresql.Driver" />
<property name="jdbcUrl" value="jdbc:postgresql://localhost:5432/db" />
<property name="user" value="user" />
<property name="password" value="pass" />
<!-- pool sizing -->
<property name="initialPoolSize" value="3" />
<property name="minPoolSize" value="6" />
<property name="maxPoolSize" value="25" />
<property name="acquireIncrement" value="3" />
<property name="maxStatements" value="0" />
</bean>
I used this and it works

java melody - spring no sql stat - without JNDI

i've got a spring maven project with javamelody.
I use hibernate with spring, don't have any JNDI data scource.
the datasource in xml conf:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://${database.location}:${database.port}/${database.dbname}?zeroDateTimeBehavior=convertToNull&characterEncoding=utf8"/>
<property name="username" value="${database.username}"/>
<property name="password" value="${database.password}"/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
<property name="persistenceUnitName" value="rtt-backend" />
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
<property name="jpaDialect" ref="jpaDialect" />
</bean>
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" />
<property name="database" value="MYSQL" />
<property name="showSql" value="false" />
</bean>
<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="dataSource" ref="dataSource" />
<property name="jpaDialect" ref="jpaDialect" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
but if i connect to javamelody monitor page says:
"sql Statistics sql - 1 day -None"
I tried :
in presistence.xml :
net.bull.javamelody.JpaPersistence
use wrapper for data source
<bean id="wrappedDataSource" class="net.bull.javamelody.SpringDataSourceFactoryBean">
<property name="targetName" value="dataSource" /> </bean>
But still nothing.
As said in the user guide, simply add in your spring context configuration:
classpath:net/bull/javamelody/monitoring-spring.xml
Or if you want to use
<bean id="wrappedDataSource" class="net.bull.javamelody.SpringDataSourceFactoryBean">
<property name="targetName" value="dataSource" />
</bean>
then replace in your entityManagerFactory and transactionManager
<property name="dataSource" ref="dataSource" />
with
<property name="dataSource" ref="wrappedDataSource" />

Why to use jpadialect in JpaTransactionManager

I am learning spring 3.0 from Spring in Action.
There it talks about importance of having jpadialect in JpaTranactionManager
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="jpaDialect" ref="jpaDialect" />
</bean>
<bean id="jpaDialect"
class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
However the dialect is already present in declaration of entityManagerFactory via jpaVendorAdaptor.
<bean id="entityManagerFactory" class= "org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
</bean>
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="HSQL" />
<property name="showSql" value="true"/>
<property name="generateDdl" value="false"/>
<property name="databasePlatform"
value="org.hibernate.dialect.HSQLDialect" />
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource. DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:hsql://localhost/spitter/spitter" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
Is the use of dialect in JpaTranactionManager redundant?
The use of jpaDialect in JpaTransactionManager is no redundant to jpaVendorAdapter config. Their config have different purpose.
You can refer this post with good explanation.

spring3 ibatis2.x multiple datasource

I use spring ibatis to connection Oracle multiple user space.
It is seems like User A,User B,User System.
The User System is always to used. the other user while opration in a Task(Thread)
spring.xml
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="System" />
<property name="password" value="system" />
</bean>
<bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="A" />
<property name="password" value="a" />
</bean>
<bean id="dataSource3" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="B" />
<property name="password" value="b" />
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:SqlMapConfig.xml" />
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="sqlMapClient1" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:SqlMapConfig.xml" />
<property name="dataSource" ref="dataSource2" />
</bean>
<bean id="sqlMapClient2" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:SqlMapConfig.xml" />
<property name="dataSource" ref="dataSource3" />
</bean>
In Dao:
public class BaseDao extends SqlMapClientDaoSupport {
Logger log = Logger.getLogger(getClass());
#Resource(name = "sqlMapClient")
private SqlMapClient sqlMapClient;
#PostConstruct
public void initSqlMapClient() {
super.setSqlMapClient(sqlMapClient);
}
}
public class UserDao extends BaseDao {
public void test()
{
this.getSqlMapClientTemplate().queryForObject("....");
}
}
Task: in taskJob i want to opration other datasorce. how to implement it?
public class TaskJob {
//#Autowired
//UserDao userDao;
public void test(){
//to get other sqlmapclient in UserDao
userDao.test();//for User A,User B,User ....
}
}
You cannot do that using SqlMapClientDaoSupport. If you are ok not to use SqlMapClientDaoSupport then the following solution should suffice.
MapClientFactoryBean should not have the data source
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:SqlMapConfig.xml" />
</bean>
Have two UserDAO(One for Each datasource)
<bean id = "userDAO" class = "com.xxxx.UserDAO">
<property name="dataSource" ref="dataSource" />
<property name="sqlMapClient" ref = "sqlMapClient">
<bean id = "userDAO1" class = "com.xxxx.UserDAO">
<property name="dataSource2" ref="dataSource" />
<property name="sqlMapClient" ref = "sqlMapClient">
</bean>
Refer here for more details http://www.mail-archive.com/user-java#ibatis.apache.org/msg04432.html
Couldnt test the code myself. Please try.

Exception on starting Tomcat

I have a web application that applies JPA, Hibernate, Spring and Wicket. When I run Tomcat, it is started, but there is an Exception:
WARN org.hibernate.ejb.packaging.InputStreamZippedJarVisitor - Unable to find file (ignored): jndi:/localhost/tracking/
java.io.FileNotFoundException
at org.apache.naming.resources.DirContextURLConnection.getInputStream(DirContextURLConnection.java:298)
at java.net.URL.openStream(URL.java:1007)
at org.hibernate.ejb.packaging.InputStreamZippedJarVisitor.doProcessElements(InputStreamZippedJarVisitor.java:38)
at org.hibernate.ejb.packaging.AbstractJarVisitor.getMatchingEntries(AbstractJarVisitor.java:139)
at org.hibernate.ejb.Ejb3Configuration.addScannedEntries(Ejb3Configuration.java:287)
at org.hibernate.ejb.Ejb3Configuration.scanForClasses(Ejb3Configuration.java:614)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:360)
at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:131)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:221)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:251)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1288)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1257)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:438)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:383)
at java.security.AccessController.doPrivileged(Native Method)...
This is my persistence.xml:
<!-- language: xml -->
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
version="1.0">
<persistence-unit name="tracking" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.eds.eai.tracking.web.model.Archive</class>
<class>com.eds.eai.tracking.web.model.Audit</class>
<class>com.eds.eai.tracking.web.model.Message</class>
<class>com.eds.eai.tracking.web.model.Partner</class>
<properties>
<property name="hibernate.dialect"
value="org.hibernate.dialect.DerbyDialect" />
<property name="hibernate.hbm2ddl.auto"
value="update" />
</properties>
</persistence-unit>
</persistence>
and this is the applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="wicketApplication"
class="com.eds.eai.tracking.web.Application">
</bean>
<!-- Using a username of logging creates the tables in a schema of the same name -->
<!-- Note the embedded driver defaults the schema to app unless a username is given -->
<bean id="db2DataSourceDev"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName"
value="com.ibm.db2.jcc.DB2Driver" />
<property name="url"
value="jdbc:db2://gotx72.got.skf.se:50000/GOT20" />
<property name="username" value="capsusr1" />
<property name="password" value="welcome3" />
<property name="poolPreparedStatements" value="true" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="10" />
</bean>
<bean id="db2DataSourceStaging"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName"
value="com.ibm.db2.jcc.DB2Driver" />
<property name="url"
value="jdbc:db2://gotx75.got.skf.se:50000/GOT20" />
<property name="username" value="capsusr1" />
<property name="password" value="welcome3" />
<property name="poolPreparedStatements" value="true" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="10" />
</bean>
<bean id="db2DataSourceMof"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName"
value="com.ibm.db2.jcc.DB2Driver" />
<property name="url"
value="jdbc:db2://ruex12.rue.skf.net:60000/RUE11" />
<property name="username" value="eai12" />
<property name="password" value="welcome3" />
<property name="poolPreparedStatements" value="true" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="10" />
</bean>
<bean id="db2DataSourceStagingLinux" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.ibm.db2.jcc.DB2Driver" />
<property name="url" value="jdbc:db2://x0015.rue.skf.net:50001/RUE22" />
<property name="username" value="capsusr1" />
<property name="password" value="Finogaratu93" />
<property name="poolPreparedStatements" value="true" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="10" />
</bean>
<bean id="entityManagerFactory1"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="db2DataSourceDev" />
<property name="persistenceXmlLocation"
value="META-INF/persistence.xml" />
<property name="loadTimeWeaver">
<bean
class="org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver" />
</property>
</bean>
<bean id="entityManagerFactory2"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="db2DataSourceStaging" />
<property name="persistenceXmlLocation"
value="META-INF/persistence.xml" />
<property name="loadTimeWeaver">
<bean
class="org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver" />
</property>
</bean>
<bean id="entityManagerFactory3"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="db2DataSourceMof" />
<property name="persistenceXmlLocation"
value="META-INF/persistence.xml" />
<property name="loadTimeWeaver">
<bean
class="org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver" />
</property>
</bean>
<bean id="entityManagerFactory4"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="db2DataSourceStagingLinux" />
<property name="persistenceXmlLocation"
value="META-INF/persistence.xml" />
<property name="loadTimeWeaver">
<bean
class="org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver" />
</property>
</bean>
<bean name="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory"
ref="entityManagerFactory1" />
</bean>
<!-- bean post-processor for JPA annotations -->
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<tx:annotation-driven />
<bean id="auditDAO"
class="com.eds.eai.tracking.web.dao.impl.AuditDAOImpl" />
<bean id="configurationDAO"
class="com.eds.eai.tracking.web.dao.impl.ConfigurationDAOImpl">
<property name="filename" value="WEB-INF/config.xml" />
</bean>
</beans>
How is this caused and how can I solve it?

Categories

Resources