I am using sqlite as db in my Micronaut application getting the sqlite busy error in the below code:
#SneakyThrows
#TransactionalAdvice(value = EmpDao.DATASOURCE, propagation = TransactionDefinition.Propagation.REQUIRES_NEW)
public void storeEmp(EmpDto empDto) {
String id = empDto.getId();
try {
if (empDao.existsById(id)) {
log.debug("updating emp for id {}", empDto.getId());
empDao.update(EmpEntity.builder()
.id(id)
.data(getJson(empDto))
.entryCreatedAt(timeService.nowDateTime().toEpochSecond(ZoneOffset.UTC))
.build());
} else {
empDao.save(
EmpEntity.builder()
.id(id)
.data(getJson(empDto))
.entryCreatedAt(timeService.nowDateTime().toEpochSecond(ZoneOffset.UTC))
.build());
}
}catch(Exception e){
log.error("emp db save/update failed for id {} ",id, e);
}
}
#SneakyThrows
#TransactionalAdvice(value = EmpDao.DATASOURCE, propagation = TransactionDefinition.Propagation.REQUIRES_NEW)
public void storeEmployees(List<Emp> empDtos) {
try {
empDao.saveAll(empDto);
} catch (Exception ex) {
log.warn("saveAll failed", ex);
empDtos.forEach(this::storeEmp);
}
}
In stacktrace I can see first saveAll getting failed becuase of Primary keyconstraint Issue, that might be because of duplicate emp ids in the list
SQL error executing INSERT: [SQLITE_CONSTRAINT_PRIMARYKEY] A PRIMARY KEY constraint failed
and After that when It tried to save/update each emp object independently through storeEmp method in forEach, it's failing with the sqlite busy exception.
What I am not sure if saveAll is already failed, how there can be multiple connection to sqlite. Can anyone suggest what's wrong with the above code.
Thanks
Related
I have a BizTalk Custom Pipeline Component that writes an SFTP file (using SSH.net), triggered by an SFTP (WinSCP) receive location.
The code within the Retry occasionally (around half the time) does not hit either the "Success" nor the logging catch block and no further processing occurs within the Pipeline. I assume that means the thread has been destroyed.
I added the Retry code later to make it try a few times but with the thread being destroyed I don't always get a success or 3 failures.
What could cause this behaviour in BizTalk 2016?
public void Archive(byte[] content,
string archivePath,
string userName,
string password,
string serverAddress,
string sshHostKeyFingerprint)
{
Retry(3, () =>
{
try
{
using (var sftpClient = new SftpClient(serverAddress, userName, password))
{
if (!string.IsNullOrWhiteSpace(sshHostKeyFingerprint))
{
sshHostKeyFingerprint = sshHostKeyFingerprint.Split(' ').Last();
sftpClient.HostKeyReceived += delegate (object sender, HostKeyEventArgs e)
{
if (e.FingerPrint.SequenceEqual(ConvertFingerprintToByteArray(sshHostKeyFingerprint)))
e.CanTrust = true;
else
e.CanTrust = false;
};
}
sftpClient.Connect();
sftpClient.WriteAllBytes(archivePath, content);
sftpClient.Disconnect();
LogInfo($"Success");
}
}
catch (Exception exception)
{
// show the bad path for "No such file" errors
throw new InvalidOperationException($"Failed to create file '{archivePath}'", exception);
}
});
}
private void Retry(int maxAttempts, Action action)
{
int attempt = 1;
while (attempt <= maxAttempts)
{
try
{
action();
break; // success
}
catch (Exception exception)
{
LogWarning($"Attempt {attempt} Error: {exception.ToString()}");
if (attempt == maxAttempts)
throw; // final attempt exception propagated
}
finally
{
attempt++;
}
}
}
I have this code in my application. If the insert fails, I would like to add information about the failure to the Audit table. Perhaps the inner exception message from the exception in the note area. Is there a way that I could do this and then still have the procedure exit with that same exception details back to the caller?
[Route("Post")]
[ValidateModel]
public async Task<IHttpActionResult> Post([FromBody]Phrase phrase)
{
phrase.StatusId = (int)EStatus.Saved;
UpdateHepburn(phrase);
db.Phrases.Add(phrase);
var audit = new Audit()
{
Entity = (int)EEntity.Phrase,
Action = (int)EAudit.Insert,
Note = phrase.English,
UserId = userId,
Date = DateTime.UtcNow,
Id = phrase.PhraseId
};
db.Audits.Add(audit);
await db.SaveChangesAsync();
return Ok(phrase);
}
You can catch the original exception and rethrow it afterwards:
try
{
await db.SaveChangesAsync();
}
catch (Exception)
{
try
{
// TODO: add to the audit here, also in a try/catch as this might fail as well
}
catch
{
}
// rethrow the original exception
throw;
}
We port our Dapper based application to .NET Core and we have a problem with our transaction code.
We use "actions" to execute stuff
public Action<IDbConnection> CreateAction(string statement, object values)
{
return (dbConnection) => dbConnection.Execute(statement, values);
}
And we use methods to execute those actions with
public void Execute(IEnumerable<Action<IDbConnection>> actions)
{
using (IDbConnection connection = OpenConnection())
using (IDbTransaction transaction = connection.BeginTransaction())
{
try
{
foreach (var action in actions)
{
action(transaction.Connection);
}
transaction.Commit();
}
catch
{
transaction.Rollback();
throw;
}
}
}
This works great with .NET Framework and Dapper 1.42, but it fails on .NET Core with Dapper 1.50.2.
System.InvalidOperationException: 'ExecuteNonQuery requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. The Transaction property of the command has not been initialized.'
If we remove the transaction using it also works fine.
What needs to change to make it work?
Ok, it seems we now have to pass the transaction explicit.
public void Execute(IEnumerable<Action<IDbConnection, IDbTransaction>> actions)
{
using (IDbConnection connection = OpenConnection())
using (IDbTransaction transaction = connection.BeginTransaction())
{
try
{
foreach (var action in actions)
{
action(transaction.Connection, transaction);
}
transaction.Commit();
}
catch
{
transaction.Rollback();
throw;
}
}
}
I have a wrapper for the webclient that I am using to retrieve some data. This same function is being used by the WP8 App and also used by the WP8 ScheduledAgent.
Somehow, when the function is used by the WP8 App, there is no error and it returns correctly.
However, when the ScheduledAgent uses the function, it erred out at the bold code below. I tried a try catch but it is not catching. Via Debugger, the GetSTringAsync(uri) had completed without any exception. The error seemed to be only happening when it is assigning the return Task to the result string.
The error I received is:
An unhandled exception of type 'System.UnauthorizedAccessException' occurred in System.Windows.ni.dll
public class HttpClient : WebClient
..
private async Task GetStringAsync(string strUri)
{
Uri uri = new Uri(strUri);
string result = string.Empty;
try
{
result = await GetStringAsync(uri);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return result;
}
...
private Task GetStringAsync(Uri requestUri)
{
TaskCompletionSource tcs = new TaskCompletionSource();
try
{
this.DownloadStringCompleted += (s, e) =>
{
if (e.Error == null)
{
tcs.TrySetResult(e.Result);
}
else
{
tcs.TrySetException(e.Error);
}
};
this.DownloadStringAsync(requestUri);
}
catch (Exception ex)
{
tcs.TrySetException(ex);
}
if (tcs.Task.Exception != null)
{
throw tcs.Task.Exception;
}
return tcs.Task;
}
Please advise if I am missing something.
My problem is because I am using pushpin as one of my object types within my Model. Apparently, in the scheduled agent, it is not able to access that object type and thus threw the above error.
im using the asp.net Enterprise Library to insert and update data to an sql database.
c# code:
Database db = DatabaseFactory.CreateDatabase();
IDbConnection connection = db.GetConnection();
connection.Open();
IDbTransaction transaction = connection.BeginTransaction();
DBCommandWrapper command;
try
{
//insert part
command = db.GetStoredProcCommandWrapper("stored_procedure1");
command.CommandTimeout = 900;
command.AddInParameter("#parameter1", DbType.Int32, 3);
db.ExecuteNonQuery(command, transaction);
//update part
command = db.GetStoredProcCommandWrapper("stored_procedure2");
command.CommandTimeout = 900;
command.AddInParameter("#param1", DbType.Int32, 5);
db.ExecuteNonQuery(command, transaction);
transaction.Commit();
}
catch (SqlException e)
{
transaction.Rollback();
throw (new ApplicationException("sql error", e));
}
catch (Exception e)
{
transaction.Rollback();
throw (new ApplicationException("error", e));
}
finally
{
connection.Close();
}
the code is on a method.
The method is executed many times and sometimes im getting error:
"Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached."
My questions are: Is it ok to have the insert and update part together as it is above?
Having the insert and update part together is causing this error?
Thanks.
First you do not roll back your transaction on error.
Secondly in order to guarantee that your finally block is called you need to add the catch block.
Try {
...
}
Catch {
}
finally {
close connection
}
Read the second paragraph of the link located below
http://msdn.microsoft.com/en-us/library/zwc8s4fz(v=vs.100).aspx