【问题标题】:XA Datasource in Microsoft JDBC driver and jTDS JDBC DriverMicrosoft JDBC 驱动程序和 jTDS JDBC 驱动程序中的 XA 数据源
【发布时间】:2013-08-12 08:56:18
【问题描述】:

我对 SQL Server 有点陌生,并且在连接到它的 java 应用程序中工作。我找到了这 2 个著名的 JDBC 驱动程序,microsoft 一个和 jTDS 一个。我正在尝试使用 XA 数据源。

根据 Microsoft 文档,here 它说我必须在使用 XA 数据源之前配置服务器。根据这个文档,它要求做的是在服务器中启用 XA。

但是我没有看到 jTDS 需要任何这样的服务器端修改。 (我找不到任何这样说的文档,here

所以,我的问题是 jTDS 本身如何管理在服务器中启用 XA,而 Microsoft 需要我来完成启用任务?

或者我在这里遗漏了什么?

【问题讨论】:

    标签: sql-server jdbc jtds xa


    【解决方案1】:

    在挖掘jTDS驱动分发后找到了答案。它有 README.XA 文件说明了一切。复制以下内容。

    XA support in jTDS
    ==================
    
    This version of jTDS includes a XADataSource class which allows the driver to
    be used with J2EE servers that only support XA JDBC connections. The class name
    is net.sourceforge.jtds.jdbcx.JtdsDataSource.
    
    By default the driver will emulate distributed transactions fooling the J2EE
    environment into believing that two phase commit is supported. This emulation
    has the serious drawback that when used in a true distributed environment, the
    driver may not be able to commit or rollback when requested by the transaction
    manager. In this situation data loss or corruption can occur.
    
    In general this emulation is safe to use when jTDS is the only transactional
    resource manager or where the database is read only and never updated.
    Emulation is much faster than support for true distributed transactions and
    this may be another reason to consider using the driver in this mode if the
    risks are understood.
    
    Many J2EE servers provide their own XA emulations that allow the normal
    net.sourceforge.jtds.jdbc.Driver to be used. The recommendation is to use the
    vendor's emulation, if available, rather than the XADataSource as this
    configuration is likely to be more stable.
    
    The driver will support true distributed transactions with MS SQL2000 provided
    that the extended stored procedure in JtdsXA.dll is installed in the target
    server. To disable the XA emulation and use the stored procedure, set the
    connection property "xaemulation" to "false". This implementation is very
    immature and cannot be recommended for production use at this stage. It may
    however be useful for development when true two phase commit support is
    required.
    
    Another connection property, "LogFile" has been added to JtdsDataSource which
    allows the jTDS logging to be enabled in a J2EE environment. The parameter
    value specifies the output file path for the logging information.
    
    Two example configurations for the popular JBoss server are included in the
    conf directory. One configuration uses the Driver interface and the server's XA
    emulation the other shows how to use the driver's XA support in either emulated
    or real mode.
    
    
    Installing JtdsXA.dll
    =====================
    
    For true distributed transaction support JtdsXA.dll has to be installed on the
    server. The installation procedure consists of two simple steps:
    
     1. Copy JtdsXA.dll from the XA directory to the <SQL_Server_Root>/binn
        directory of the SQL Server installation.
    
     2. From the command prompt run the following command in the directory where
        you extracted jTDS:
    
        isql -Usa -P<sa_password> -S<server_name_or_ip> -iXA\instjtds.sql
    
        Alternatively, use any DB tool (such as Query Analyzer or any JDBC tool) to
        execute the script within the 'master' database. Don't forget to log in as
        'sa'.
    
    This will install the xp_jtdsxa extended stored procedure, used by jTDS to
    provide true distributed transacition support.
    

    【讨论】:

      【解决方案2】:

      您将使用 sqljdbc_4.0 驱动程序 (http://www.microsoft.com/en-us/download/details.aspx?id=11774)。 这里解释 XA 数据源以及如何在服务器中启用它。这对我有用。

      -- This script installs the extended stored procedures that implement
      -- distributed transaction and XA support for the Microsoft JDBC Driver 4.0 for SQL Server.
      -- Works only with SQL 2005 and above
      
      -- Notes for SQL Administrators:
      
      -- #1. Prior to running this script you must copy the extended stored procedure dll SQLJDBC_XA.dll 
      --     to the target SQL Server's Binn folder.
      
      -- #2. Permissions to the distributed transaction support procedures for the Microsoft JDBC Driver 4.0 
      --     for SQL Server are granted through the SQL Server role [SqlJDBCXAUser].  To maintain a secure default 
      --     configuration, no user is granted access to this role by default.
      
      -- Drop and re-create the extended stored procedure definitions in master.
      
      use master
      go
      
      -- Drop any existing procedure definitions.
      if exists (select * from sys.objects where object_id = object_id('xp_sqljdbc_xa_init') and OBJECTPROPERTY(object_id, N'IsExtendedProc') = 1) exec sp_dropextendedproc 'xp_sqljdbc_xa_init' 
      if exists (select * from sys.objects where object_id = object_id('xp_sqljdbc_xa_start') and OBJECTPROPERTY(object_id, N'IsExtendedProc') = 1) exec sp_dropextendedproc 'xp_sqljdbc_xa_start'
      if exists (select * from sys.objects where object_id = object_id('xp_sqljdbc_xa_end') and OBJECTPROPERTY(object_id, N'IsExtendedProc') = 1) exec sp_dropextendedproc 'xp_sqljdbc_xa_end'
      if exists (select * from sys.objects where object_id = object_id('xp_sqljdbc_xa_prepare') and OBJECTPROPERTY(object_id, N'IsExtendedProc') = 1) exec sp_dropextendedproc 'xp_sqljdbc_xa_prepare'
      if exists (select * from sys.objects where object_id = object_id('xp_sqljdbc_xa_commit') and OBJECTPROPERTY(object_id, N'IsExtendedProc') = 1) exec sp_dropextendedproc 'xp_sqljdbc_xa_commit'
      if exists (select * from sys.objects where object_id = object_id('xp_sqljdbc_xa_rollback') and OBJECTPROPERTY(object_id, N'IsExtendedProc') = 1) exec sp_dropextendedproc 'xp_sqljdbc_xa_rollback'
      if exists (select * from sys.objects where object_id = object_id('xp_sqljdbc_xa_forget') and OBJECTPROPERTY(object_id, N'IsExtendedProc') = 1) exec sp_dropextendedproc 'xp_sqljdbc_xa_forget'
      if exists (select * from sys.objects where object_id = object_id('xp_sqljdbc_xa_recover') and OBJECTPROPERTY(object_id, N'IsExtendedProc') = 1) exec sp_dropextendedproc 'xp_sqljdbc_xa_recover'
      if exists (select * from sys.objects where object_id = object_id('xp_sqljdbc_xa_rollback_ex') and OBJECTPROPERTY(object_id, N'IsExtendedProc') = 1) exec sp_dropextendedproc 'xp_sqljdbc_xa_rollback_ex'
      if exists (select * from sys.objects where object_id = object_id('xp_sqljdbc_xa_forget_ex') and OBJECTPROPERTY(object_id, N'IsExtendedProc') = 1) exec sp_dropextendedproc 'xp_sqljdbc_xa_forget_ex'
      if exists (select * from sys.objects where object_id = object_id('xp_sqljdbc_xa_prepare_ex') and OBJECTPROPERTY(object_id, N'IsExtendedProc') = 1) exec sp_dropextendedproc 'xp_sqljdbc_xa_prepare_ex'
      if exists (select * from sys.objects where object_id = object_id('xp_sqljdbc_xa_init_ex') and OBJECTPROPERTY(object_id, N'IsExtendedProc') = 1) exec sp_dropextendedproc 'xp_sqljdbc_xa_init_ex'
      go
      
      -- Install the procedures.
      exec sp_addextendedproc 'xp_sqljdbc_xa_init', 'SQLJDBC_XA.dll'
      exec sp_addextendedproc 'xp_sqljdbc_xa_start', 'SQLJDBC_XA.dll'
      exec sp_addextendedproc 'xp_sqljdbc_xa_end', 'SQLJDBC_XA.dll'
      exec sp_addextendedproc 'xp_sqljdbc_xa_prepare', 'SQLJDBC_XA.dll'
      exec sp_addextendedproc 'xp_sqljdbc_xa_commit', 'SQLJDBC_XA.dll'
      exec sp_addextendedproc 'xp_sqljdbc_xa_rollback', 'SQLJDBC_XA.dll'
      exec sp_addextendedproc 'xp_sqljdbc_xa_forget', 'SQLJDBC_XA.dll'
      exec sp_addextendedproc 'xp_sqljdbc_xa_recover', 'SQLJDBC_XA.dll'
      exec sp_addextendedproc 'xp_sqljdbc_xa_rollback_ex', 'SQLJDBC_XA.dll'
      exec sp_addextendedproc 'xp_sqljdbc_xa_forget_ex', 'SQLJDBC_XA.dll'
      exec sp_addextendedproc 'xp_sqljdbc_xa_prepare_ex', 'SQLJDBC_XA.dll'
      exec sp_addextendedproc 'xp_sqljdbc_xa_init_ex', 'SQLJDBC_XA.dll'
      go
      
      -- Create the [SqlJDBCXAUser] role in master database.
      -- The SQL administrator can later add users to this role to allow users to participate 
      -- in Microsoft JDBC Driver 4.0 for SQL Server distributed transactions.
      if exists (select * from sys.schemas where name = 'SqlJDBCXAUser' ) 
      drop schema [SqlJDBCXAUser];
      
      if exists (select * from sys.database_principals where name = 'SqlJDBCXAUser' and type='R') 
      drop role [SqlJDBCXAUser];
      
      create role [SqlJDBCXAUser]
      go
      
      
      -- Grant privileges to [SqlJDBCXAUser] role to the extended stored procedures.
      grant execute on xp_sqljdbc_xa_init to [SqlJDBCXAUser]
      grant execute on xp_sqljdbc_xa_start to [SqlJDBCXAUser]
      grant execute on xp_sqljdbc_xa_end to [SqlJDBCXAUser]
      grant execute on xp_sqljdbc_xa_prepare to [SqlJDBCXAUser]
      grant execute on xp_sqljdbc_xa_commit to [SqlJDBCXAUser]
      grant execute on xp_sqljdbc_xa_rollback to [SqlJDBCXAUser]
      grant execute on xp_sqljdbc_xa_recover to [SqlJDBCXAUser]
      grant execute on xp_sqljdbc_xa_forget to [SqlJDBCXAUser]
      grant execute on xp_sqljdbc_xa_rollback_ex to [SqlJDBCXAUser]
      grant execute on xp_sqljdbc_xa_forget_ex to [SqlJDBCXAUser]
      grant execute on xp_sqljdbc_xa_prepare_ex to [SqlJDBCXAUser]
      grant execute on xp_sqljdbc_xa_init_ex to [SqlJDBCXAUser]
      go
      
      -- Add users to the [SqlJDBCXAUser] role as needed.
      
      -- Example for adding a SQL authentication user to the SqlJDBCXAUser role.
      -- exec sp_addrolemember [SqlJDBCXAUser], 'MySQLUser'
      
      -- Example for adding a windows domain user to the SqlJDBCXAUser role.
      -- exec sp_addrolemember [SqlJDBCXAUser], 'MyDomain\MyWindowsUser'
      
      print ''
      print 'SQLJDBC XA DLL installation script complete.'
      print 'Check for any error messages generated above.'
      

      【讨论】:

      • 您误解了这个问题。感谢您的回复。
      猜你喜欢
      • 2017-01-09
      • 2011-12-12
      • 1970-01-01
      • 2017-05-05
      • 2014-02-04
      • 2012-09-16
      • 1970-01-01
      • 2012-11-26
      • 2011-05-22
      相关资源
      最近更新 更多