I have one method that I need that be run synchronized (If 10 users call this method the first runs the method and the 9 remaining wait he finish, then the second executes the method ant the 8 remaining wait...)
So this method need to save one register in my postgresql, but I need to begin and commit the transaction inside this method, so search on internet I found this solution:
@org.springframework.transaction.annotation.Transactional(isolation = Isolation.SERIALIZABLE, propagation = Propagation.REQUIRES_NEW)
private synchronized  Registro gerarRegistro(Protocolo protocolo, List<ServicoCalculado> servicos,String especialidade, LocalDateTime dtRegistro) throws Exception{
    Registro novoRegistro = new Registro();
    //this method executes one method in my service that call a method in my repository with this query -> @Query(value = "SELECT MAX(numero_registro) FROM rtdpj.registro WHERE tp_especialidade = ?1", nativeQuery = true)
    Long ultimoRegistro = this.registroService.findLastRegistro(especialidade);     
    ultimoRegistro++;
    novoRegistro.setEspecialidade(especialidade);
    novoRegistro.setNrPastaPj(protocolo.getPastaPJ());
    novoRegistro.setNumeroRegistro(ultimoRegistro);
    novoRegistro.setRegistro(registroService.gerarRegistro(ultimoRegistro, novoRegistro.getDataRegistro()));
    novoRegistro.setProtocolo(protocolo.getId());
    novoRegistro.setRegistroReferencia(protocolo.getRegistroReferencia());
    novoRegistro.setObjetos(protocolo.getObjetos());
    novoRegistro.setSituacaoAtual(protocolo.getSituacaoAtualRegistro());
    novoRegistro.setObservacao(protocolo.getObservacaoRegistro());
    novoRegistro.setServicos(servicos);
    novoRegistro.setProtocoloCancelamentoIndisponibilidade(protocolo.getProtocoloCancelamentoIndisponibilidade());
    novoRegistro.setProtocoloIndisponibilidade(protocolo.getProtocoloIndisponibilidade());
    novoRegistro.setNatureza(protocolo.getNatureza());
    novoRegistro.setCliente(protocolo.getCliente());
    novoRegistro.setIcGuardaConservacao(protocolo.getIcGuardaConservacao());
    novoRegistro.setIcPossuiSigiloLegal(protocolo.getIcPossuiSigiloLegal());
    novoRegistro.setNumeroRegistroReferencia(protocolo.getNumeroRegistroReferencia());
    return this.registroService.save(novoRegistro);
}
It's the best way to does this? or have a better way?
In my domain the propertie novoRegistro.setNumeroRegistro must be sequential withou jumps, so If I use SERIAL and at the moment that saves the register something happend I will lost the nextval, and I have to keep the client's arrival order FIRST to IN is the FIRST to get a number
 
    