Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quartz JobStoreCMT Integration #21400

Open
1 task done
NunoGamaVieira opened this issue Nov 20, 2024 · 0 comments
Open
1 task done

Quartz JobStoreCMT Integration #21400

NunoGamaVieira opened this issue Nov 20, 2024 · 0 comments

Comments

@NunoGamaVieira
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

I am trying to get the queuing of a job to be set in the same transaction as the request.

Describe the solution you'd like

I see that Quartz already supports this but there is no easy way to configure it in ABP. I came out with a solution but wanted to ask if you see any issues with it or a better way to do it.
As Quartz has it, this class has to handle connections for storing and running jobs.

public class DBContextJobStoreCMT : JobStoreCMT
{
    private readonly IUnitOfWorkManager _unitOfWorkManager;

    public DBContextJobStoreCMT(IUnitOfWorkManager unitOfWorkManager)
    {
        _unitOfWorkManager = unitOfWorkManager;
        OpenConnection = true;
    }


    protected override ConnectionAndTransactionHolder GetNonManagedTXConnection()
    {
        IUnitOfWork uow = _unitOfWorkManager.Current;
        if (uow != null)
        {
            IReadOnlyList<IDatabaseApi> databaseapis = ((UnitOfWork)uow).GetAllActiveDatabaseApis();
            if (databaseapis.Count == 1)
            {
                var DatabaseAux = databaseapis.FirstOrDefault();
                if (DatabaseAux is EfCoreDatabaseApi)
                {
                    var dbConnectionAux = ((EfCoreDatabaseApi)DatabaseAux).DbContext.Database.GetDbConnection();
                    if (dbConnectionAux.State != System.Data.ConnectionState.Open)
                    {
                        dbConnectionAux.OpenAsync().Wait();
                    }
                    var transactionAux = ((EfCoreDatabaseApi)DatabaseAux).DbContext.Database.CurrentTransaction?.GetDbTransaction();
                    return new UOWConnectionAndTransactionHolder(dbConnectionAux, transactionAux);
                }
            }
        }

     
        return base.GetNonManagedTXConnection();

    }

    protected override void CloseConnection(ConnectionAndTransactionHolder connectionAndTransactionHolder)
    {
        if (!(connectionAndTransactionHolder is UOWConnectionAndTransactionHolder))
        {
            connectionAndTransactionHolder.Connection.Close();
        }
    }
}


public class UOWConnectionAndTransactionHolder : ConnectionAndTransactionHolder
{
    public UOWConnectionAndTransactionHolder(DbConnection connection, DbTransaction transaction) : base(connection, transaction)
    {
    }
}

}

Additional context

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant