zoukankan      html  css  js  c++  java
  • Spring事务

    Spring 事务配置说明

    Spring 如果没有特殊说明,一般指是跟数据存储有关的数据操作事务操作;对于数据持久操作的事务配置,一般有三个对象,数据源,事务管理器,以及事务代理机制;

    Spring 提供了多种的底层数据源实现,以及多种类型的事务管理器;所有的管理器都基于 PlatformTransactionManager 接口实现各自的事务策略;

    Spring 事务管理采用 AOP 切面代理技术实现,AOP 用于分隔关注点,保证事务的原子性,采用一定的技术 把该关注点 (weaving) 织入到 待完善的关注点上,实现单独组件无法实现的功能,以解决面向对象编程在某些方式下难于实现的操作,更好的支持面向对象的开关原则(扩展开放,修改关闭)。

    底层数据源配置

    首选加载 数据源配置 .properties 文件;

    <bean id="loadProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <list>
                    <value>classpath:META-INF/mybatis/mysql.properties</value>
                    <value>classpath:META-INF/spring/hibernate.properties</value>
                </list>
            </property>
    </bean>

    mysql.properties:

    driver=com.mysql.jdbc.Driver
    url=jdbc:mysql://localhost:3306/springdb
    username=root
    password=xxxxx
    filters=stat
    initialSize=2
    maxActive=300
    maxWait=60000
    timeBetweenEvictionRunsMillis=60000
    minEvictableIdleTimeMillis=300000
    validationQuery=SELECT 1
    testWhileIdle=true
    testOnBorrow=false
    testOnReturn=false
    poolPreparedStatements=false
    maxPoolPreparedStatementPerConnectionSize=200

    hibernate.properties:

    # hibernate.X
    hibernate.connection.driverClass=org.gjt.mm.mysql.Driver
    hibernate.connection.url=jdbc:mysql://localhost:3306/springdb?useUnicode=true&amp;characterEncoding=utf-8
    hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
    hibernate.connection.username=root
    hibernate.connection.password=xxxxx
    hibernate.show_sql=true
    hibernate.hbm2ddl.auto=create-drop

    1. JDBC 方式:

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="${driver}"></property>
            <property name="url" value="${url}"></property>
            <property name="username" value="${username}"></property>
            <property name="password" value="${password}"></property>
    </bean>

    2. c3p0 方式:

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
            destroy-method="close">
            <!-- 指定连接数据库的驱动 -->
            <property name="driverClass" value="com.mysql.jdbc.Driver"/>
            <!-- 指定连接数据库的URL -->
            <property name="jdbcUrl" value="jdbc:mysql://localhost/springdb"/>
            <!-- 指定连接数据库的用户名 -->
            <property name="user" value="root"/>
            <!-- 指定连接数据库的密码 -->
            <property name="password" value="xxxxx"/>
            <!-- 指定连接数据库连接池的最大连接数 -->
            <property name="maxPoolSize" value="40"/>
            <!-- 指定连接数据库连接池的最小连接数 -->
            <property name="minPoolSize" value="1"/>
            <!-- 指定连接数据库连接池的初始化连接数 -->
            <property name="initialPoolSize" value="1"/>
            <!-- 指定连接数据库连接池的连接的最大空闲时间 -->
            <property name="maxIdleTime" value="20"/>
    </bean>

    3. dbcp 方式:

    <beans>
      <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
       <property name="driverClassName" value="${driver}"></property>
       <property name="url" value="${url}"></property>
       <property name="username" value="${username}"></property>
       <property name="password" value="${password}"></property>
    </bean>

    4. Alibaba Druid 方式:

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close" init-method="init" >
            <property name="url" value="${url}?useUnicode=true&amp;characterEncoding=utf-8"></property>
            <property name="driverClassName" value="${driver}"></property>
            <property name="username" value="${username}"></property>
            <property name="password" value="${password}"></property>
            <property name="filters" value="${filters}"></property>
            <property name="maxActive" value="${maxActive}"></property>
            <property name="maxWait" value="${maxWait}"></property>
            <property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}"></property>
            <property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}"></property>
            <property name="validationQuery" value="${validationQuery}"></property>
            <property name="testWhileIdle" value="${testWhileIdle}"></property>
            <property name="testOnBorrow" value="${testOnBorrow}"></property>
            <property name="testOnReturn" value="${testOnReturn}"></property>
            <property name="poolPreparedStatements" value="${poolPreparedStatements}"></property>
            <property name="maxPoolPreparedStatementPerConnectionSize" value="${maxPoolPreparedStatementPerConnectionSize}"></property>
    </bean>

    5. JNDI 全局配置方式:

    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">      
         <property name="jndiName" value="${jndiName}"></property> 
    </bean>

    6. 自定义 DataSource:

    <bean id="dataSource" class="me.study.hnmapper.utils.CustomDataSource">
       <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"></property>
       <property name="driverUrl" value="jdbc:oracle:thin:@10.30.2.204:1527:sec"></property>
       <property name="username" value="apps"></property>
       <property name="password" value="secapp29"></property>
    </bean>

    CustomDataSource:

    package me.study.hnmapper.utils;
    
    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.SQLFeatureNotSupportedException;
    import java.util.logging.Logger;
    
    import javax.sql.DataSource;
    
    public class CustomDataSource implements DataSource {
    
        private String driverClass;
        private String driverUrl;
        private String username;
        private String password;
    
        @Override
        public PrintWriter getLogWriter() throws SQLException {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public void setLogWriter(PrintWriter out) throws SQLException {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void setLoginTimeout(int seconds) throws SQLException {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public int getLoginTimeout() throws SQLException {
            // TODO Auto-generated method stub
            return 0;
        }
    
        @Override
        public Logger getParentLogger() throws SQLFeatureNotSupportedException {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public <T> T unwrap(Class<T> iface) throws SQLException {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public boolean isWrapperFor(Class<?> iface) throws SQLException {
            // TODO Auto-generated method stub
            return false;
        }
    
        @Override
        public Connection getConnection() throws SQLException {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public Connection getConnection(String username, String password)
                throws SQLException {
            // TODO Auto-generated method stub
            // TODO Auto-generated method stub
            Connection conn = null;
            try {
                Class.forName(driverClass);
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            conn = DriverManager.getConnection(driverUrl, username, password);
    
            return conn;
        }
    
        public String getDriverClass() {
            return driverClass;
        }
    
        public void setDriverClass(String driverClass) {
            this.driverClass = driverClass;
        }
    
        public String getDriverUrl() {
            return driverUrl;
        }
    
        public void setDriverUrl(String driverUrl) {
            this.driverUrl = driverUrl;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
    }

    6. Hibernate方式,利用的是SessionFactory作为数据源操作;

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <property name="mappingLocations" >
                <list>
                    <value>classpath*:model/*.hbm.xml</value>
                </list>
            </property>
            <!-- packagesToScan可以自动搜索某个package的全部标记@Entity class -->
            <!-- 
            <property name="packagesToScan">
                <list>
                    <value>hibernatelibs.model*</value>
                </list>
            </property>
             -->
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                    <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                    <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                </props>
            </property>
    </bean>
  • 相关阅读:
    经典解析23 种设计模式
    如何用手机赚钱——admob
    解决solaris zfs文件系统内存占用过高
    在运行suncluster的数据库服务器上遇到oracle故障的解决办法
    对oracle用户进行资源限制
    Linux下TortoiseGit 配置管理环境搭建
    Linux系统非root帐号执行ssh发生Segmentation fault错误
    Solaris的panic故障分析
    转: Linux下单网卡多vlan多虚拟机
    oracle故障快速定位
  • 原文地址:https://www.cnblogs.com/cw172/p/11795847.html
Copyright © 2011-2022 走看看