I am new to spring.
I have multiple databases but any given time one database is open depending on which database user select. So I have a bean defined as below. So my question is how do I instantiate the datasource user select and use it in my class if I use below given bean. Any ideas would be appreciated.
<bean id="dataSources" class="java.util.TreeMap">
<constructor-arg>
<map>
<entry key="dp1www" value-ref="dataSourceDp1www" />
<entry key="dp2www" value-ref="dataSourceDp2www" />
<entry key="sp0www" value-ref="dataSourceSp0www" />
<entry key="sp1www" value-ref="dataSourceSp1www" />
<entry key="sp2www" value-ref="dataSourceSp2www" />
</map>
</constructor-arg>
</bean>
<bean id="dataSourceDp1www" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="jdbc:oracle:thin:#sf1-hab-ppb-ww.home.com:1525/dp1www1.home.com" />
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="username" value="select" />
<property name="password" value="######" />
</bean>
<bean id="dataSourceDp2www" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="jdbc:oracle:thin:#sf1-hbl-ppb-ww.home.com:1525/dp2www1.home.com" />
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="username" value="select" />
<property name="password" value="######" />
</bean>
<bean id="dataSourceSp0www" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="jdbc:oracle:thin:#sf1-hbl-ppb-ww.home.com:1525/sp0www1.home.com" />
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="username" value="select" />
<property name="password" value="######" />
</bean>
<bean id="dataSourceSp1www" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="jdbc:oracle:thin:#sf1-hab-ppb-ww.home.com:1525/sp1www1.home.com" />
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="username" value="select" />
<property name="password" value="######" />
</bean>
You can just use #Autowired annotation to inject TreeMap instance:
#Autowired
private TreeMap<String, DataSource> dataSources;
....
DataSource dataSource = dataSources.get("dp1www");
or you can directly inject specific data source without even creating TreeMap
#Autowired
private DataSource dataSourceDp1www;
Related
I'm setting up two different datasource for diff databse in spring-servlet.xml
I have tried multiple solutions provided on sites most of them are for spring-boot.
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:#localhost:1521:xe" />
<property name="username" value="***" />
<property name="password" value="***" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="dataSourceDev"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:#localhost:1521:xe" />
<property name="username" value="***" />
<property name="password" value="***" />
</bean>
<bean id="jdbcTemplateDev" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSourceDev" />
</bean>
In java class -
#Autowired
#Qualifier("jdbcTemplate")
private JdbcTemplate jdbcTemplate;
#Autowired
#Qualifier("jdbcTemplateDev")
private JdbcTemplate jdbcTemplatedev;*/
I want to use mysql database in spring batch application. Where should I keep my XML file with the details of datasource.
currently i have kept the env-context.xml in src/main/resources/META-INF/batch/override/manager
Content of env-context.xml:
<!-- connect to database -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/demo_db" />
<property name="username" value="testuser" />
<property name="password" value="12345" />
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
The common approach is to put this kind of configuration parameters into a properties file in order to externalize them as they will change from one environment to another.
It can be done as next:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:datasource.properties</value>
</property>
</bean>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${datasource.driver}" />
<property name="url" value="${datasource.url}" />
<property name="username" value="${datasource.username}" />
<property name="password" value="${datasource.password}" />
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
Then you put your file datasource.properties into the classpath, this file will have a content of type:
datasource.driver=com.mysql.jdbc.Driver
datasource.url=jdbc:mysql://localhost:3306/demo_db
datasource.username=testuser
datasource.password=12345
i am having a web application and my application is connected to database for every hit to my web application i need to make a connection to database so i want to reduce time by caching or pooling connection
below is my database configured file
xml file:
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/"/>
<property name="username" value="root"/>
<property name="password" value="password"/>
</bean>
Please use HikariCP for your application, best and very fast connection pool available in java world,configuration should be like below.
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" >
<constructor-arg>
<bean class="com.zaxxer.hikari.HikariConfig">
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/" />
<property name="maximumPoolSize" value="20" />
<property name="username" value="root" />
<property name="password" value="root" />
<property name="poolName" value="my-pool1" />
</bean>
</constructor-arg>
</bean>
Or you can have programatic configuration as well.
HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:mysql://localhost:3306/simpsons");
config.setUsername("bart");
config.setPassword("51mp50n");
config.addDataSourceProperty("cachePrepStmts", "true");
config.addDataSourceProperty("prepStmtCacheSize", "250");
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
HikariDataSource ds = new HikariDataSource(config);
You can use as follows , just change your database driver and url settings
<bean id="springDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" >
<property name="url" value="jdbc:oracle:thin:#localhost:1521:Employee" />
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="username" value="scott" />
<property name="password" value="tiger" />
<property name="removeAbandoned" value="true"/>
<property name="initialSize" value="20" />
<property name="maxActive" value="30" />
</bean>
You should use SingleConnectionDataSource as datasource
<bean id="dataSource"
class="org.springframework.jdbc.datasource.SingleConnectionDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/"/>
<property name="username" value="root"/>
<property name="password" value="password"/>
</bean>
I am using org.apache.commons.dbcp.BasicDataSource and com.mchange.v2.c3p0.ComboPooledDataSource APIs to manage datasource connections.
When I have changed the correct connection parameters I was expecting these API gave an exception during initialization but they did not. What am I missing?
<bean id="uslDataSource1" class="org.apache.commons.dbcp.BasicDataSource" lazy-init="false">
<property name="driverClassName" value="${usl.db.driverClass}"/>
<property name="url" value="${usl.db.jdbcUrl}"/>
<property name="username" value="${usl.db.username}"/>
<property name="password" value="${usl.db.password}"/>
<property name="initialSize" value="${usl.db.initialPoolSize}"/>
<property name="validationQuery" value="select 1 from dualx"/>
<property name="testOnBorrow" value="false"/>
<property name="testWhileIdle" value="true"/>
<property name="timeBetweenEvictionRunsMillis" value="1200000"/>
<property name="minEvictableIdleTimeMillis" value="1800000"/>
<property name="numTestsPerEvictionRun" value="5"/>
<property name="defaultAutoCommit" value="true"/>
</bean>
<!-- the DataSource (parameterized for configuration via a PropertyPlaceHolderConfigurer) -->
<bean id="scpcDataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource" lazy-init="false">
<property name="driverClass" value="${scpc.db.driverClass}" />
<property name="jdbcUrl" value="${scpc.db.jdbcUrl}" />
<property name="user" value="${scpc.db.username}" />
<property name="password" value="${scpc.db.password}" />
<property name="initialPoolSize" value="${scpc.db.initialPoolSize}" />
<property name="minPoolSize" value="${scpc.db.minPoolSize}" />
<property name="maxPoolSize" value="${scpc.db.maxPoolSize}" />
<property name="acquireIncrement" value="${scpc.db.acquireIncrement}" />
<property name="autoCommitOnClose" value="${scpc.db.autoCommitOnClose}" />
<!-- property name="maxIdleTime" value="${scpc.db.maxIdleTime}" / -->
<property name="idleConnectionTestPeriod" value="${scpc.db.idleConnectionTestPeriod}" />
<property name="preferredTestQuery" value="${scpc.db.validationQuery}" />
<property name="maxStatements" value="${scpc.db.maxStatements}" />
</bean>
I am creating a database connection pool with following properties.
<bean id="complianceCCRDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" lazy-init="false" >
<property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" />
<property name="username" value="${deshaw.compliance.ccr.db.username}" />
<property name="password" value="${deshaw.compliance.ccr.db.password}" />
<property name="url" value="${deshaw.compliance.ccr.db.url}" />
<property name="initialSize" value="5" />
<property name="maxActive" value="10" />
<property name="maxWait" value="60000"/>
<property name="testOnBorrow" value="true" />
<property name="validationQuery">
<value>use ${deshaw.compliance.ccr.db.name} SELECT 1
</value>
</property>
<property name="maxIdle" value="10" />
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="600" />
<property name="logAbandoned" value="false" />
</bean>
When I try to instantiate the above bean, I am getting following messages on STDOUT. Is there any way I can suppress these ?
AbandonedObjectPool is used (org.apache.commons.dbcp.AbandonedObjectPool#2d82ef6b)
LogAbandoned: false
RemoveAbandoned: true
RemoveAbandonedTimeout: 600
You should use the setLogWriter() (see javadoc here) method as described in this JIRA.