spring3 ibatis2.x multiple datasource - java

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.

Related

How to use multiple datasources in the same class?

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>

java.lang.IllegalAccessError: org.apache.commons.dbcp.DelegatingPreparedStatement.isClosed()Z

i tried to make integration of spring and mybatis.
jdk:1.8
run my test:
#Test
public void testFindUserById() throws Exception{
UserMapper userMapper=(UserMapper)applicationContext.getBean("userMapper");
User user=userMapper.findUserById(1);
System.out.println(user);
}
and error:the full stacktrace
java.lang.IllegalAccessError: org.apache.commons.dbcp.DelegatingPreparedStatement.isClosed()Z
The spring configuration file:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/custom?useSSL=false" />
<property name="username" value="root" />
<property name="password" value="qqwe5631652" />
<property name="maxIdle" value="5" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:SqlMapConfig.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="mapper" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
<bean id="userDao" class="dao.UserDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
</beans>
file's structure
all of ‘.jar’
It 's java.lang.IllegalAccessError about authority?
i have no idea
i change commons-dbcp-1.2.1.jar into commons-dbcp-1.4.jar ,and it 's ok now!
Yeah, your solution was right.
If you take a look to DelegatingPreparedStatement.isClosed() javadoc you can see that this method is protected so any attempt from your side to invoke this method will end up in an IllegalAccessException because you don't have permissions to do this. Newer versions of this library has turned this metod up public

Connecting Java Web Application to Oracle Database

<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName"
value="????????????????????" />
<property name="url"
value="???????????????????????"/>
<property name="username" value="root" />
<property name="password" value="" />
</bean>
I am not able to figure out what should go in driver class name value and url vale. I have downloaded Oracle sql developer and oracle 11 g.But i am not sure how to configure it to my java application
Driver class name:- oracle.jdbc.driver.OracleDriver
URL :- jdbc:oracle:thin:#(hostname):(port number):(database name)
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName"
value="oracle.jdbc.driver.OracleDriver" />
<property name="url"
value="jdbc:oracle:thin:#(hostname):(port number):(database name)"/>
<property name="username" value="root" />
<property name="password" value="" />
</bean>
Your qustion is not clear if you are using Spring framework there are two type of
Data source handling technique
Method 1 you need to configure data source in your application server
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:jboss/datasources/OracleDS" />
<property name="cache" value="false"/>
<property name="lookupOnStartup" value="false"/>
<property name="proxyInterface" value="javax.sql.DataSource"/>
</bean>
Method 2
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:#127.0.0.1:1521:OMQA" />
<property name="username" value="root" />
<property name="password" value="password" />
</bean>

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.

Categories

Resources