热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

深入解析Java的Spring框架中的混合事务与bean的区分

这篇文章主要介绍了Java的Spring框架中的混合事务与bean的区分,Spring是Java的SSH三大web开发框架之一,需要的朋友可以参考下

混合事务
在ORM框架的事务管理器的事务内,使用JdbcTemplate执行SQL是不会纳入事务管理的。
下面进行源码分析,看为什么必须要在DataSourceTransactionManager的事务内使用JdbcTemplate。

1.开启事务
DataSourceTransactionManager

     protected void doBegin(Object transaction,TransactionDefinition definition) {
          DataSourceTransactiOnObjecttxObject= (DataSourceTransactionObject) transaction;
          Connection con = null;
 
          try {
              if(txObject.getConnectionHolder() == null ||
                        txObject.getConnectionHolder().isSynchronizedWithTransaction()){
                   COnnectionnewCon= this.dataSource.getConnection();
                   if(logger.isDebugEnabled()) {
                        logger.debug("AcquiredConnection [" + newCon + "] for JDBC transaction");
                   }
                   txObject.setConnectionHolder(newConnectionHolder(newCon), true);
              }
 
              txObject.getConnectionHolder().setSynchronizedWithTransaction(true);
              con =txObject.getConnectionHolder().getConnection();
 
              IntegerpreviousIsolatiOnLevel= DataSourceUtils.prepareConnectionForTransaction(con,definition);
              txObject.setPreviousIsolationLevel(previousIsolationLevel);
 
              // Switch to manualcommit if necessary. This is very expensive in some JDBC drivers,
              // so we don't wantto do it unnecessarily (for example if we've explicitly
              // configured theconnection pool to set it already).
              if(con.getAutoCommit()) {
                   txObject.setMustRestoreAutoCommit(true);
                   if(logger.isDebugEnabled()) {
                        logger.debug("SwitchingJDBC Connection [" + con + "] to manual commit");
                   }
                   con.setAutoCommit(false);
              }
              txObject.getConnectionHolder().setTransactionActive(true);
 
              int timeout =determineTimeout(definition);
              if (timeout !=TransactionDefinition.TIMEOUT_DEFAULT) {
                   txObject.getConnectionHolder().setTimeoutInSeconds(timeout);
              }
 
              // Bind the sessionholder to the thread.
              if(txObject.isNewConnectionHolder()) {
                   TransactionSynchronizationManager.bindResource(getDataSource(),txObject.getConnectionHolder());
              }
          }
 
          catch (Exception ex) {
              DataSourceUtils.releaseConnection(con,this.dataSource);
              throw newCannotCreateTransactionException("Could not open JDBC Connection fortransaction", ex);
          }
     }

doBegin()方法会以数据源名为Key,ConnectionHolder(保存着连接)为Value,将已经开启事务的数据库连接绑定到一个ThreadLocal变量上。

2.绑定连接

     public static void bindResource(Objectkey, Object value) throws IllegalStateException {
          Object actualKey =TransactionSynchronizationUtils.unwrapResourceIfNecessary(key);
          Assert.notNull(value,"Value must not be null");
          Map map = resources.get();
          // set ThreadLocal Map ifnone found
          if (map == null) {
              map = newHashMap();
              resources.set(map);
          }
          Object oldValue = map.put(actualKey, value);
          // Transparently suppress aResourceHolder that was marked as void...
          if (oldValue instanceofResourceHolder && ((ResourceHolder) oldValue).isVoid()) {
              oldValue = null;
          }
          if (oldValue != null) {
              throw newIllegalStateException("Already value [" + oldValue + "] for key[" +
                        actualKey+ "] bound to thread [" + Thread.currentThread().getName() +"]");
          }
          if (logger.isTraceEnabled()){
              logger.trace("Boundvalue [" + value + "] for key [" + actualKey + "] to thread[" +
                        Thread.currentThread().getName()+ "]");
          }
     }

resources变量就是上面提到的ThreadLocal变量,这样后续JdbcTemplate就可以用DataSource作为Key,查找到这个数据库连接。

3.执行SQL
JdbcTemplate

     public Objectexecute(PreparedStatementCreator psc, PreparedStatementCallback action)
              throwsDataAccessException {
 
          Assert.notNull(psc,"PreparedStatementCreator must not be null");
          Assert.notNull(action,"Callback object must not be null");
          if (logger.isDebugEnabled()){
              String sql =getSql(psc);
              logger.debug("Executingprepared SQL statement" + (sql != null ? " [" + sql +"]" : ""));
          }
 
          Connection con = DataSourceUtils.getConnection(getDataSource());
          PreparedStatement ps = null;
          try {
              Connection cOnToUse= con;
              if(this.nativeJdbcExtractor != null &&
                        this.nativeJdbcExtractor.isNativeConnectionNecessaryForNativePreparedStatements()){
                   cOnToUse=this.nativeJdbcExtractor.getNativeConnection(con);
              }
              ps =psc.createPreparedStatement(conToUse);
              applyStatementSettings(ps);
              PreparedStatementpsToUse = ps;
              if(this.nativeJdbcExtractor != null) {
                   psToUse =this.nativeJdbcExtractor.getNativePreparedStatement(ps);
              }
              Object result =action.doInPreparedStatement(psToUse);
              handleWarnings(ps);
              return result;
          }
          catch (SQLException ex) {
              // ReleaseConnection early, to avoid potential connection pool deadlock
              // in the case whenthe exception translator hasn't been initialized yet.
              if (psc instanceofParameterDisposer) {
                   ((ParameterDisposer)psc).cleanupParameters();
              }
              String sql =getSql(psc);
              psc = null;
              JdbcUtils.closeStatement(ps);
              ps = null;
              DataSourceUtils.releaseConnection(con,getDataSource());
              con = null;
              throwgetExceptionTranslator().translate("PreparedStatementCallback", sql,ex);
          }
          finally {
              if (psc instanceofParameterDisposer) {
                   ((ParameterDisposer)psc).cleanupParameters();
              }
              JdbcUtils.closeStatement(ps);
              DataSourceUtils.releaseConnection(con,getDataSource());
          }
     }


4.获得连接
DataSourceUtils

    public static Connection doGetConnection(DataSourcedataSource) throws SQLException {
          Assert.notNull(dataSource,"No DataSource specified");
 
          ConnectionHolder cOnHolder= (ConnectionHolder)TransactionSynchronizationManager.getResource(dataSource);
          if (conHolder != null&& (conHolder.hasConnection() ||conHolder.isSynchronizedWithTransaction())) {
              conHolder.requested();
              if(!conHolder.hasConnection()) {
                   logger.debug("Fetchingresumed JDBC Connection from DataSource");
                   conHolder.setConnection(dataSource.getConnection());
              }
              returnconHolder.getConnection();
          }
          // Else we either got noholder or an empty thread-bound holder here.
 
          logger.debug("FetchingJDBC Connection from DataSource");
          Connection con =dataSource.getConnection();
 
          if (TransactionSynchronizationManager.isSynchronizationActive()){
              logger.debug("Registeringtransaction synchronization for JDBC Connection");
              // Use sameConnection for further JDBC actions within the transaction.
              // Thread-boundobject will get removed by synchronization at transaction completion.
              COnnectionHolderholderToUse= conHolder;
              if (holderToUse ==null) {
                   holderToUse= new ConnectionHolder(con);
              }
              else {
                   holderToUse.setConnection(con);
              }
              holderToUse.requested();
              TransactionSynchronizationManager.registerSynchronization(
                        newConnectionSynchronization(holderToUse, dataSource));
              holderToUse.setSynchronizedWithTransaction(true);
              if (holderToUse !=conHolder) {
                   TransactionSynchronizationManager.bindResource(dataSource,holderToUse);
              }
          }
 
          return con;
     }

由此可见,DataSourceUtils也是通过TransactionSynchronizationManager获得连接的。所以只要JdbcTemplate与DataSourceTransactionManager有相同的DataSource,就一定能得到相同的数据库连接,自然就能正确地提交、回滚事务。
 
再以Hibernate为例来说明开篇提到的问题,看看为什么ORM框架的事务管理器不能管理JdbcTemplate。

5 ORM事务管理器
HibernateTransactionManager

if(txObject.isNewSessionHolder()) { 
     TransactionSynchronizationManager.bindResource(getSessionFactory(),txObject.getSessionHolder()); 
} 

因为ORM框架都不是直接将DataSource注入到TransactionManager中使用的,而是像上面Hibernate事务管理器一样,使用自己的SessionFactory等对象来操作DataSource。所以尽管可能SessionFactory和JdbcTemplate底层都是一样的数据源,但因为在TransactionSynchronizationManager中绑定时使用了不同的Key(一个是sessionFactory名,一个是dataSource名),所以JdbcTemplate执行时是拿不到ORM事务管理器开启事务的那个数据库连接的。


bean的区分
一个公共工程中的Spring配置文件,可能会被多个工程引用。因为每个工程可能只需要公共工程中的一部分Bean,所以这些工程的Spring容器启动时,需要区分开哪些Bean要创建出来。
1.应用实例
以Apache开源框架Jetspeed中的一段配置为例:page-manager.xml
 

 
  
  
   
  
  
   
  
  ……
 
 
 
  
  
  
   JETSPEED-INF/ojb/page-manager-repository.xml
  
  
  
   
  
  ……
 

2.Bean过滤器
JetspeedBeanDefinitionFilter在Spring容器解析每个Bean定义时,会取出上面Bean配置中j2:cat对应的值,例如dbPageManageror pageSerializer。然后将这部分作为正则表达式与当前的Key(从配置文件中读出)进行匹配。只有匹配上的Bean,才会被Spring容器创建出来。
 
JetspeedBeanDefinitionFilter

  public boolean match(BeanDefinition bd)
  {
    String beanCategoriesExpression = (String)bd.getAttribute(CATEGORY_META_KEY);
    boolean matched = true;
    if (beanCategoriesExpression != null)
    {
      matched = ((matcher != null)&& matcher.match(beanCategoriesExpression));
    }
    return matched;
}
 
  public void registerDynamicAlias(BeanDefinitionRegistry registry, String beanName,BeanDefinition bd)
  {
    String aliases =(String)bd.getAttribute(ALIAS_META_KEY);
    if (aliases != null)
    {
      StringTokenizer st = newStringTokenizer(aliases, " ,");
      while (st.hasMoreTokens())
      {
        String alias = st.nextToken();
        if (!alias.equals(beanName))
        {
          registry.registerAlias(beanName, alias);
        }
      }
    }
  }

match()方法中的CATEGORY_META_KEY的值就是j2:cat,matcher类中保存的就是当前的Key,并负责将当前Key与每个Bean的进行正则表达式匹配。
registerDynamicAlias的作用是:在Bean匹配成功后,定制的Spring容器会调用此方法为Bean注册别名。详见下面1.3中的源码。

3.定制Spring容器
定制一个Spring容器,重写registerBeanDefinition()方法,在Spring注册Bean时进行拦截。

public class FilteringXmlWebApplicationContextextends XmlWebApplicationContext
{
  private JetspeedBeanDefinitionFilterfilter;
  
  publicFilteringXmlWebApplicationContext(JetspeedBeanDefinitionFilter filter, String[]configLocations, Properties initProperties, ServletContext servletContext)
  {
    this(filter, configLocations,initProperties, servletContext, null);
  }
  
  publicFilteringXmlWebApplicationContext(JetspeedBeanDefinitionFilter filter, String[]configLocations, Properties initProperties, ServletContext servletContext,ApplicationContext parent)
  {
    super();
    if (parent != null)
    {
      this.setParent(parent);
    }
    if (initProperties != null)
    {
      PropertyPlaceholderConfigurer ppc =new PropertyPlaceholderConfigurer();
      ppc.setIgnoreUnresolvablePlaceholders(true);
      ppc.setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_FALLBACK);
      ppc.setProperties(initProperties);
      addBeanFactoryPostProcessor(ppc);
    }
    setConfigLocations(configLocations);
    setServletContext(servletContext);
    this.filter = filter;
  }
  
  protected DefaultListableBeanFactorycreateBeanFactory()
  {
    return new FilteringListableBeanFactory(filter,getInternalParentBeanFactory());
  }
}
 
public classFilteringListableBeanFactory extends DefaultListableBeanFactory
{
  private JetspeedBeanDefinitionFilterfilter;
  
  public FilteringListableBeanFactory(JetspeedBeanDefinitionFilterfilter, BeanFactory parentBeanFactory)
  {
    super(parentBeanFactory);
    this.filter = filter;
    if (this.filter == null)
    {
      this.filter = newJetspeedBeanDefinitionFilter();
    }
    this.filter.init();
  }
 
  /**
   * Override of the registerBeanDefinitionmethod to optionally filter out a BeanDefinition and
   * if requested dynamically register anbean alias
   */
  public void registerBeanDefinition(StringbeanName, BeanDefinition bd)
      throws BeanDefinitionStoreException
  {
    if (filter.match(bd))
    {
      super.registerBeanDefinition(beanName, bd);
      if (filter != null)
      {
        filter.registerDynamicAlias(this, beanName, bd);
      }
    }
  }
}

4.为Bean起别名
使用BeanReferenceFactoryBean工厂Bean,将上面配置的两个Bean(xmlPageManager和dbPageManager)包装起来。将Key配成各自的,实现通过配置当前Key来切换两种实现。别名都配成一个,这样引用他们的Bean就直接引用这个别名就行了。例如下面的PageLayoutComponent。
 
page-manager.xml


  
  
  
 
 
 
  
  
  
 
 
 
  
  
   
  
  
   jetspeed-layouts::VelocityOneColumn
  
  


推荐阅读
  • 一、Hadoop来历Hadoop的思想来源于Google在做搜索引擎的时候出现一个很大的问题就是这么多网页我如何才能以最快的速度来搜索到,由于这个问题Google发明 ... [详细]
  • Spring常用注解(绝对经典),全靠这份Java知识点PDF大全
    本文介绍了Spring常用注解和注入bean的注解,包括@Bean、@Autowired、@Inject等,同时提供了一个Java知识点PDF大全的资源链接。其中详细介绍了ColorFactoryBean的使用,以及@Autowired和@Inject的区别和用法。此外,还提到了@Required属性的配置和使用。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • 本文介绍了Hyperledger Fabric外部链码构建与运行的相关知识,包括在Hyperledger Fabric 2.0版本之前链码构建和运行的困难性,外部构建模式的实现原理以及外部构建和运行API的使用方法。通过本文的介绍,读者可以了解到如何利用外部构建和运行的方式来实现链码的构建和运行,并且不再受限于特定的语言和部署环境。 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • Android系统移植与调试之如何修改Android设备状态条上音量加减键在横竖屏切换的时候的显示于隐藏
    本文介绍了如何修改Android设备状态条上音量加减键在横竖屏切换时的显示与隐藏。通过修改系统文件system_bar.xml实现了该功能,并分享了解决思路和经验。 ... [详细]
  • Python SQLAlchemy库的使用方法详解
    本文详细介绍了Python中使用SQLAlchemy库的方法。首先对SQLAlchemy进行了简介,包括其定义、适用的数据库类型等。然后讨论了SQLAlchemy提供的两种主要使用模式,即SQL表达式语言和ORM。针对不同的需求,给出了选择哪种模式的建议。最后,介绍了连接数据库的方法,包括创建SQLAlchemy引擎和执行SQL语句的接口。 ... [详细]
  • mac php错误日志配置方法及错误级别修改
    本文介绍了在mac环境下配置php错误日志的方法,包括修改php.ini文件和httpd.conf文件的操作步骤。同时还介绍了如何修改错误级别,以及相应的错误级别参考链接。 ... [详细]
  • 一句话解决高并发的核心原则
    本文介绍了解决高并发的核心原则,即将用户访问请求尽量往前推,避免访问CDN、静态服务器、动态服务器、数据库和存储,从而实现高性能、高并发、高可扩展的网站架构。同时提到了Google的成功案例,以及适用于千万级别PV站和亿级PV网站的架构层次。 ... [详细]
  • 延迟注入工具(python)的SQL脚本
    本文介绍了一个延迟注入工具(python)的SQL脚本,包括使用urllib2、time、socket、threading、requests等模块实现延迟注入的方法。该工具可以通过构造特定的URL来进行注入测试,并通过延迟时间来判断注入是否成功。 ... [详细]
  • 本文介绍了一个免费的asp.net控件,该控件具备数据显示、录入、更新、删除等功能。它比datagrid更易用、更实用,同时具备多种功能,例如属性设置、数据排序、字段类型格式化显示、密码字段支持、图像字段上传和生成缩略图等。此外,它还提供了数据验证、日期选择器、数字选择器等功能,以及防止注入攻击、非本页提交和自动分页技术等安全性和性能优化功能。最后,该控件还支持字段值合计和数据导出功能。总之,该控件功能强大且免费,适用于asp.net开发。 ... [详细]
  • 背景应用安全领域,各类攻击长久以来都危害着互联网上的应用,在web应用安全风险中,各类注入、跨站等攻击仍然占据着较前的位置。WAF(Web应用防火墙)正是为防御和阻断这类攻击而存在 ... [详细]
  • Activiti7流程定义开发笔记
    本文介绍了Activiti7流程定义的开发笔记,包括流程定义的概念、使用activiti-explorer和activiti-eclipse-designer进行建模的方式,以及生成流程图的方法。还介绍了流程定义部署的概念和步骤,包括将bpmn和png文件添加部署到activiti数据库中的方法,以及使用ZIP包进行部署的方式。同时还提到了activiti.cfg.xml文件的作用。 ... [详细]
  • Android日历提醒软件开源项目分享及使用教程
    本文介绍了一款名为Android日历提醒软件的开源项目,作者分享了该项目的代码和使用教程,并提供了GitHub项目地址。文章详细介绍了该软件的主界面风格、日程信息的分类查看功能,以及添加日程提醒和查看详情的界面。同时,作者还提醒了读者在使用过程中可能遇到的Android6.0权限问题,并提供了解决方法。 ... [详细]
  • 本文详细介绍了Mybatis中#与$的区别及其作用。#{}可以防止sql注入,拼装sql时会自动添加单引号,适用于单个简单类型的形参。${}则将拿到的值直接拼装进sql,可能会产生sql注入问题,需要手动添加单引号,适用于动态传入表名或字段名。#{}可以实现preparedStatement向占位符中设置值,自动进行类型转换,有效防止sql注入,提高系统安全性。 ... [详细]
author-avatar
书友49537618
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有