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
Related
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://xxx.xx.xxx.xxx:3306/vod6?autoReconnect=true" />
<!-- <property name="url" value="jdbc:mysql://xxx.xx.xxx.xxx:3306/vod2?autoReconnect=true" /> -->
<property name="username" value="voddb" />
<property name="password" value="vod#123" />
</bean>
above config xml working fine .
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${mysql.driverClassName}" />
<property name="url" value="${mysql.url}" />
<property name="username" value="${mysql.username}" />
<property name="password" value="${mysql.password}" />
</bean>
When I am trying to configure jdbc.property and configure them throwing error out of memory
Can you please explain what i am missing?
mysql.database=MYSQL
mysql.driverClassName=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://xxx.xx.xxx.xxx:3306/vod?autoReconnect=true
mysql.username=voddb
mysql.password=vod#123
mysql.initialSize=4
mysql.maxActive=30
property file
Please confirm whether the placeholder is mentioned correctly in applicationContext.xml file
<context:property-placeholder location="classpath*:catalina.properties" ignore-unresolvable="true"/>
If this is configured correctly please try removing mysql.initialSize=4 mysql.maxActive=30 from properties file and test, this may be thr root cause. Because this is the additional property you have mentioned in properties file
You can try configuring these maxActive,initialSize values in xml itself as property.
<property name="removeAbandonedTimeout" value="180"/>
<property name="removeAbandoned" value="true"/>
<property name="logAbandoned" value="true"/>
<property name="timeBetweenEvictionRunsMillis" value="5000"/>
<property name="minEvictableIdleTimeMillis" value="900000"/>
<property name="maxActive" value="20"/>
<property name="maxIdle" value="4"/>
I am using spring MVC 4.2 I have this code in my app-context.xml
<bean id="beanConstants" name="beanConstants" class="com.my.web.controller.BeanConstants">
<property name="dbProplocation" value="/my/database.properties" />
<property name="extractDbProplocation" value="/my/extract.database.properties" />
<property name="cssLocation" value="uncompiled" />
<property name="enableSuspensionPollingStr" value="false" />
</bean>
<!-- JDBC Session Config -->
<bean class="org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessionConfiguration"/>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="${hibernate.connection.url}" />
<property name="username" value="${hibernate.connection.username}" />
<property name="password" value="${hibernate.connection.password}" />
</bean>
Now this all worked fine until I added the JdbcHttpSessionConfiguration bean. If I take that line out, it works. otherwise I get this:
Caused by: java.lang.IllegalArgumentException:
Could not resolve placeholder 'hibernate.connection.url' in string value "${hibernate.connection.url}"
I am trying to save the sesstion info in the DB. Why is this doing this? What has one got to do with the other?
I'm failling to use Spring4 togather with Hibernate4 in a standalone process (no container like tomcat, WAS, ...)
How can I use Hibernate4, Spring4 and Spring data repositories togather in a standalone process?
However I confiugre Spring I allways get the same exception:
Caused by: java.lang.NullPointerException
at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.getStatus(JtaStatusHelper.java:76)
at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.isActive(JtaStatusHelper.java:118)
at org.hibernate.engine.transaction.internal.jta.CMTTransaction.join(CMTTransaction.java:149)
When googling for this, I get pointet to some information about hibernate.transaction.jta.platform and the docu for Hibernate 4.3 is here http://docs.jboss.org/hibernate/orm/4.3/devguide/en-US/html_single/#services-JtaPlatform
But the only option I see for my case would be org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform but this still leads to the same error.
Here is my Spring config:
<context:component-scan base-package="com.xx.yy" />
<jpa:repositories base-package="com.xx.zz.respositories"></jpa:repositories>
<bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/culture" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jtaDataSource" ref="dataSource" />
<property name="packagesToScan" value="culture.matching.index.model" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="true" />
<property name="showSql" value="false" />
<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
<property name="database" value="MYSQL" />
</bean>
</property>
<property name="jpaProperties">
<value>
hibernate.transaction.jta.platform=org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform
</value>
</property>
</bean>
Answer by #geoand helped a lot: https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-simple
I therefore moved from XML to Java config
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.
I am using Spring, ibatis for ORM. My app-config.xml look like
<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://192.168.10.50/lmexdb_v1" />
<property name="username" value="lmexdba" />
<property name="password" value="lmexdba123#" />
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation"
value="classpath:com/platysgroup/lmex/server/mobile/dao/ibatis/SqlMapConfig.xml" />
</bean>
<bean id="mobileController" class="com.platysgroup.lmex.server.controller.MobileController">
<property name="announcementService" ref="announcementService"></property>
<property name="courseService" ref="courseService"></property>
<property name="userService" ref="userService"></property>
</bean>
and I have my sqlmapconfig.xml file in src/webapp/spring.
But when I run my application on tomcat it show me a exception:
java.io.FileNotFoundException: class path resource [com/platysgroup/lmex/server/mobile/dao/ibatis/SqlMapConfig.xml] cannot be opened because it does not exist
put it in src , and it will be available
if you are using maven project then add it to resource