Invoices with consecutive multiuser [closed]

-1

Good as you can control one when 2 people try to save an invoice in the same instant in mysql or in sql. Now try with autonumerics and with max (id), also try a counter in a table but always throw an exception when you try to access at the same time from 2 pc. Is there some way or some algorithm to control that and that they are assigned consecutive valid without losing consecutive on the way?

    
asked by arsoft cr 12.07.2018 в 19:57
source

1 answer

0

This sounds like a coding error rather than concurrency, it would be nice if you could put the code you use to open the connection to the database, if you try to use the same connection to save the data at the same time it is likely that have the problem you mention, I recommend you implement a factory model on a singelton to prevent the problem:

public static class DataFactory
{
    public static SqlConnection GetConexion(string connectionString)
    {
        var connection = new SqlConnection(connectionString);
        connection.Open();

        return connection;
    }
}

And you implement it like this:

public class Repository
{
    public void DataOperation()
    {
        //Con using aseguras que tu conexion se cierre una vez que termina tu metodo o funcion
        using(var connection = DataFactory.GetConexion("tucadenadeconexion"))
        {
            /*Tu logica*/
        }
    }
}

I also recommend reading a bit about the differences between a static and non-static class.

    
answered by 12.07.2018 / 20:39
source