spring配置ibatis的jdbc方式和proxool连接池方式
来源:互联网 发布:thewolf软件平台 编辑:程序博客网 时间:2024/04/19 21:13
spring配置ibatis的jdbc方式和proxool连接池方式,以sqlserver2005为例,驱动为sqljdbc.jar
jdbc.properties中内容如下
jdbc.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.url=jdbc/:sqlserver/://localhost/:1433; DatabaseName/=IBATIS
jdbc.username=sa
jdbc.password=123456
jdbc.url=jdbc/:sqlserver/://localhost/:1433; DatabaseName/=IBATIS
jdbc.username=sa
jdbc.password=123456
1 直接用jdbc方式(这种方式适合开发阶段,发布的程序强烈要求用连接池),代码如下,这个都知道,没什么可说的
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="com.mydomain">
<context:include-filter type="aspectj" expression="com.mydomain.spring..*"/>
</context:component-scan>
<context:property-placeholder location="classpath:jdbc.properties" />
<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="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:com/mydomain/data/SqlMapConfig.xml"/>
<property name="dataSource" ref="dataSource"/>
</bean>
<!--ibatis的事务管理直接用的DataSourceTransactionManager,因为ibatis没有自己的事务管理器-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<aop:config>
<aop:pointcut id="baseServiceMethods"
expression="execution(* com.mydomain.spring.*.*(..))" />
<aop:advisor advice-ref="txAdvice"
pointcut-ref="baseServiceMethods" />
</aop:config>
<aop:aspectj-autoproxy />
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="select*" read-only="true" propagation="REQUIRED"/>
<tx:method name="find*" read-only="true" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
<tx:method name="update*" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
<tx:method name="add*" propagation="REQUIRED" isolation="REPEATABLE_READ" />
<tx:method name="delete*" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
</tx:attributes>
</tx:advice>
</beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="com.mydomain">
<context:include-filter type="aspectj" expression="com.mydomain.spring..*"/>
</context:component-scan>
<context:property-placeholder location="classpath:jdbc.properties" />
<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="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:com/mydomain/data/SqlMapConfig.xml"/>
<property name="dataSource" ref="dataSource"/>
</bean>
<!--ibatis的事务管理直接用的DataSourceTransactionManager,因为ibatis没有自己的事务管理器-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<aop:config>
<aop:pointcut id="baseServiceMethods"
expression="execution(* com.mydomain.spring.*.*(..))" />
<aop:advisor advice-ref="txAdvice"
pointcut-ref="baseServiceMethods" />
</aop:config>
<aop:aspectj-autoproxy />
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="select*" read-only="true" propagation="REQUIRED"/>
<tx:method name="find*" read-only="true" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
<tx:method name="update*" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
<tx:method name="add*" propagation="REQUIRED" isolation="REPEATABLE_READ" />
<tx:method name="delete*" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
</tx:attributes>
</tx:advice>
</beans>
SqlMapConfig.xml内容如下:
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<sqlMap resource="com/mydomain/data/Account.xml"/>
</sqlMapConfig>
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<sqlMap resource="com/mydomain/data/Account.xml"/>
</sqlMapConfig>
2 用proxool连接池方式,只有datasource发生变化,其他的无变动
<bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource" destroy-method="close">
<property name="alias" value="test"></property>
<property name="delegateProperties">
<value>user=${jdbc.username},password=${jdbc.password}</value>
</property>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="driver" value="${jdbc.driverClassName}"/>
<property name="driverUrl" value="${jdbc.url}"/>
<property name="houseKeepingTestSql" value="select CURRENT_DATE"></property>
//此处继续增加proxool属性,详细见proxool文档
</bean>
<property name="alias" value="test"></property>
<property name="delegateProperties">
<value>user=${jdbc.username},password=${jdbc.password}</value>
</property>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="driver" value="${jdbc.driverClassName}"/>
<property name="driverUrl" value="${jdbc.url}"/>
<property name="houseKeepingTestSql" value="select CURRENT_DATE"></property>
//此处继续增加proxool属性,详细见proxool文档
</bean>
此处说明一下:属性中的user和password不起任何作用,需要用delegateProperties方式写一下,否则会报错误,如下
org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Connection for transaction; nested exception is com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user ''. The user is not associated with a trusted SQL Server connection.
但是user和password还不能被去掉。
除了上面的用delegateProperties之外,还可以将用户名和密码直接写在url后面。
http://www.blogjava.net/landor2004/archive/2009/07/01/284978.html
- spring配置ibatis的jdbc方式和proxool连接池方式
- java application应用程序 使用JDBC和proxool两种方式连接数据库 的测试程序代码
- java application应用程序 使用JDBC和proxool两种方式连接数据库 的测试程序代码
- JDBC proxool连接池配置
- proxool连接池的配置【结合spring和hibernate】
- proxool连接池配置说明(与jdbc、Hibernate、Spring之间的配置)
- spring下的proxool连接池配置
- spring中以dataSource方式使用proxool连接池
- spring中以dataSource方式使用proxool连接池
- spring中以dataSource方式使用proxool连接池
- spring中以dataSource方式使用proxool连接池
- 在JDBC和Spring中使用proxool连接池
- Spring 配置proxool连接池
- Spring配置proxool连接池
- spring(数据库)jdbc配置、spring与jdbc编程的结合和jdbc模板方式的使用
- Spring、proxool和tomcat Jndi方式整合
- Spring中使用proxool连接池的配置方式与出现Attempt to refer to a unregistered pool by its alias 'ums'异常的解决办法
- Spring+Hibernate 配置和使用c3p0和proxool连接池
- 没水准
- 《程序员》杂志调价啦!
- mount命令详解
- 使用AjaxPro,js调用后台方法
- 连连看(C#版)—第8关图片移动算法
- spring配置ibatis的jdbc方式和proxool连接池方式
- 智力题
- 连连看(C#版)——部分代码1
- 笔记 02_传智播客hibernate教程_hibernate入门案例的细节分析
- 80后乡村女校长-城里收购旧书
- .NET中class和struct的区别
- 通过控件名称 获取控件
- 很牛的一段代码
- Chinese Battle asp.net battle.net officially open