Rails 3 - ธุรกรรมและการล็อค

ฉันยังใหม่กับ Rails และมีระบบที่ต้องประมวลผลธุรกรรม ผู้ใช้สามารถเข้าสู่ธุรกรรมที่มีผู้ใช้มากกว่าหนึ่งรายผูกอยู่ ผู้ใช้เหล่านี้เป็นหนี้เงินจำนวนหนึ่งกับบุคคลที่ทำธุรกรรม ตัวอย่างเช่น บิลอาจซื้ออาหารกลางวันให้เพื่อน 4 คนและใบเรียกเก็บเงินคือ 125 ดอลลาร์ พวกเขาตัดสินใจแบ่งบิล 5 วิธี ดังนั้นแต่ละวิธีจึงเป็นหนี้ 25 ดอลลาร์ Bill จะป้อนยอดรวม $125 และระบุเพื่อนแต่ละคน (รวมถึงตัวเขาเองด้วย) เนื่องจากเป็นหนี้ $25 ในการทำธุรกรรม ฉันมีโค้ดในคอนโทรลเลอร์และในโมเดลของฉันเพื่อให้บรรลุเป้าหมายนี้ แต่ฉันไม่รู้จริงๆ ว่าฉันใช้ธุรกรรมและล็อคอย่างถูกต้องหรือไม่ นอกจากนี้ นี่เป็นวิธีที่ต้องการให้มีข้อมูลนี้ในคอนโทรลเลอร์หรือไม่ ฉันกำลังใช้ธุรกรรมเนื่องจากการกระทำเหล่านี้ทั้งหมดจะต้องเกิดขึ้นพร้อมกันหรือล้มเหลว (อะตอมมิกซิตี) และฉันต้องการล็อคในกรณีที่ผู้ใช้หลายคนพยายามส่งที่ ในเวลาเดียวกัน (แยก) บางทีฉันควรปล่อยให้ db บนตัวจัดการแบ็กเอนด์ล็อค? มันทำอย่างนั้นแล้ว - พูด MySQL เหรอ? ขอบคุณ.

trans_controller.rb

class TransController < ApplicationController
    # POST trans/
    def create
        @title = "Create Transaction"
        trans_successful = false
        
        # Add the transaction from the client
        @tran = Tran.new(params[:tran])

        # Update the current user
        @tran.submitting_user_id = current_user.id
        
        # Update the data to the database
        # This call commits the transaction and transaction users 
        # It also calls a method to update the balances of each user since that isn't
        # part of the regular commit (why isn't it?)
        begin 
            @tran.transaction do
                @tran.save! 
                @tran.update_user_balances
                trans_successful = true
            end 
        rescue
            
        end
        
        # Save the transaction
        if trans_successful
            flash[:success] = 'Transaction was successfully created.'
            redirect_to trans_path
        else
            flash.now[:error] = @tran.errors.full_messages.to_sentence          
            render 'new'
        end
    end

ทราน.rb

class Tran < ActiveRecord::Base
    has_many :transaction_users, :dependent => :destroy, :class_name => 'TransactionUser'
    belongs_to :submitting_user, :class_name => 'User'
    belongs_to :buying_user, :class_name => 'User'
    
    accepts_nested_attributes_for :transaction_users, :allow_destroy => true

    validates :description, :presence => true,
                            :length => {:maximum => 100 }
    #validates :total,      :presence => true
    validates_numericality_of :total, :greater_than => 0
    
    validates :submitting_user_id,      :presence => true               
    validates :buying_user_id,          :presence => true   
            
    #validates_associated :transaction_users
    
    validate :has_transaction_users?
    validate :has_correct_transaction_user_sum?
    validate :has_no_repeat_users?
    
    def update_user_balances
        # Update the buying user in the transaction
        self.buying_user.lock!
        # Update the user's total, since they were in the transction
        self.buying_user.update_attribute :current_balance, self.buying_user.current_balance - self.total
        # Add an offsetting transaction_user for this record
        buying_tran_user = TransactionUser.create!(:amount => -1 * self.total, :user_id => self.buying_user_id, :tran => self)
        #if buying_tran_user.valid?
        #   raise "Error"
        #end
        
        # Loop through each transaction user and update their balances.  Make sure to lock each record before doing the update.
        self.transaction_users.each do |tu|
            tu.user.lock!
            tu.user.update_attribute :current_balance, tu.user.current_balance + tu.amount
        end
    end
    
    def has_transaction_users?
        errors.add :base, "A transcation must have transaction users." if self.transaction_users.blank?
    end
    
    def has_correct_transaction_user_sum?
        sum_of_items = 0;
        
        self.transaction_users.inspect
        self.transaction_users.each do |key|
            sum_of_items += key.amount if !key.amount.nil?
        end
        
        if sum_of_items != self.total
            errors.add :base, "The transcation items do not sum to the total of the transaction." 
        end 
    end
     
    def has_no_repeat_users?
        user_array = []
        self.transaction_users.each do |key|
            if(user_array.include? key.user.email) 
                errors.add :base, "The participant #{key.user.full_name} has been listed more than once."
            end
        
            user_array << key.user.email
        end
    end 
end

person skaz    schedule 26.04.2011    source แหล่งที่มา


คำตอบ (1)


ฉันจะหลีกเลี่ยงการทำการล็อคด้วยตนเองเนื่องจาก mysql จะจัดการการล็อคระดับแถวที่จำเป็นอย่างถูกต้องภายในธุรกรรม การใช้ธุรกรรมถูกต้องในกรณีนี้ สิ่งที่ฉันจะหลีกเลี่ยงคือการสร้างตัวแปรท้องถิ่นเพื่อติดตามว่าธุรกรรมเสร็จสมบูรณ์โดยไม่มีข้อผิดพลาดหรือไม่:

def create
    @title = "Create Transaction"

    # Add the transaction from the client
    @tran = Tran.new(params[:tran])

    # Update the current user
    @tran.submitting_user_id = current_user.id

    # Update the data to the database
    # This call commits the transaction and transaction users 
    # It also calls a method to update the balances of each user since that isn't
    # part of the regular commit (why isn't it?)
    begin 
        @tran.transaction do
            @tran.save! 
            @tran.update_user_balances
            trans_successful = true
        end 
    rescue
        flash.now[:error] = @tran.errors.full_messages.to_sentence          
        render 'new'
    else
        flash[:success] = 'Transaction was successfully created.'
        redirect_to trans_path
    end
end
person Wes    schedule 26.04.2011