SQL Timeout when running an Entity Framework Migration - entity-framework

I am currently experiencing a SQL Timeout when running a SQL() command inside of an EF Migration.
Situation:
I am replacing a bunch (>50) tables with one table, and need to convert the data from those tables I'm about to drop into the new table. I've organized the Migration the following way:
1.Create the new table.
In the same Migration, use the SQL() function to run a sql script that migrates the data.
3.Drop all the old tables.
Currently, the migration gives the following error:
System.Data.SqlClient.SqlException (0x80131904): Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
The statement has been terminated. ---> System.ComponentModel.Win32Exception (0x80004005): The wait operation timed out
The error is happening in an environment where I give them an installer, and they run it without me involved, so I can't manually run individual migrations, and pause in the middle to run the SQL script.
Is there any way to change the timeout for a connection, or get around this issue?
Environment:
EF 6.0 Code First
SQL Server 2012

See this answer.
Use Configuration.cs file to set custom time out:
internal sealed class Configuration :
DbMigrationsConfiguration<ApplicationDbContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
ContextKey = "YourDbContext";
// New timeout in seconds
this.CommandTimeout = 60 * 5;
}
}
With this method, you can change the timeout only for migration and not everyone using your default connection string.

You should be able to set the connection timeout in the connection string, something like:
Connection Timeout=180;

Related

SocketException thrown when using linq for Npgsql EntityFramework6 model

After much effort getting my MVC5 vb.net app configured with Npgsql and EntityFramework6.Npgsql I can connect to the postgres database fine using the Entity Data Model Wizard in Visual Studio 2017, creating a new connection with Data Provider of PostgreSQL Database, entering my database server details and testing the connection (succeeds). I can also double-click the EDMX file and add tables, etc as expected. This is my first project that talks to a postgreSQL database (normally use MS SQL Server). Everything seems to be happy and fine until I encounter code that is a linq statement that attempts to query the database. My Linq statement is:
Dim l_ListOfPwrCycleCrashEvents As List(Of pwr_cycle_crash_events) = (From item In m_AnalyticsEntities.pwr_cycle_crash_events Where item.serialnumber = "0123445678" Select item).ToList
I have the statement in a try/catch block, but visual studio still reports the following:
Exception thrown:'System.Net.Sockets.SocketException' in System.dll Additional
information: A non-blocking socket operation could not be completed
immediately occurred
I cannot seem to figure out why this statement fails. I am able to use similar statements against a different EntityFramework (MS SQL Server) that is also configured in the application.
The complete method that contains the linq statement:
Public ReadOnly Property iCountOfPwrCycleCrashEvents(
ByVal a_sSerialNumber As String
) As Integer Implements IAnalytics.iCountOfPwrCycleCrashEvents
Get
Try
Dim l_ListOfPwrCycleCrashEvents As List(Of pwr_cycle_crash_events) = (From item In m_AnalyticsEntities.pwr_cycle_crash_events
Where item.serialnumber = a_sSerialNumber
Select item).ToList
If (l_ListOfPwrCycleCrashEvents IsNot Nothing) Then Return l_ListOfPwrCycleCrashEvents.Count
Catch l_Exception2 As System.Net.Sockets.SocketException
Dim askjfhakjh As Integer = 7
Catch l_Exception As Exception
Dim kjhadkjh As Integer = 1
End Try
Return 0
End Get
End Property
I am using Npgsql v4.1.1 and EntityFramework6.Npgsql v6.3.0 in an application that targets .NET Framework 4.5.1.
I can't seem to figure out why the linq statement throws this exception or how to solve the problem. What suggestions do you have?
I figured out the issue. I was using 4.5.1 framework because I thought I had a dependency that would not allow me to move to 4.5.2. I went to Package Manager Console and ran the following command:
Update-Package -ProjectName myProjectName -reinstall
By doing so, I found that I had a package that was needed but could not be loaded because it required the 4.5.2 framework (or higher). I changed the framework of the app to 4.5.2 and issued the above Package Manager command and it executed without errors. I ran the app and the linq statement no longer throws an exception.

Flyway - Flyway Schema migration failed

I have successfully configured spring boot with a new project to work
with flyway
Migrated with the Postgres database from the version 0001.0 to 0008.0
I have made manually alter the script in local but
flyway migration getting failed.
Sample Error message:
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'flywayInitializer' defined in class path
resource
[org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]:
Invocation of init method failed; nested exception is
org.flywaydb.core.api.FlywayException: Validate failed: Migration
checksum mismatch for migration version 0006.0
How to alter the database tables without affecting flyway script from the flyway_schema_history?
For example, I need to change the table name using alter command but executing the flyway migration script without failed.
Any suggestions, Kindly appreciated.
Note:- I don't want to remove the script entries from the table flyway_schema_history.
There are a few ways to do this:-
1) Create a new script file with incremented version. Put your DDL commands to alter the table in this file. Then run migration.
2) If you don't want to delete the entry from the schema_version table, you can change the checksum value in that table. To calculate checksum, use the following method copied from org.flywaydb.core.internal.resolver.sql.SqlMigrationResolver. You can pass null for resource parameter:-
/**
* Calculates the checksum of this string.
*
* #param str The string to calculate the checksum for.
* #return The crc-32 checksum of the bytes.
*/
/* private -> for testing */
static int calculateChecksum(Resource resource, String str) {
final CRC32 crc32 = new CRC32();
BufferedReader bufferedReader = new BufferedReader(new StringReader(str));
try {
String line;
while ((line = bufferedReader.readLine()) != null) {
crc32.update(line.getBytes("UTF-8"));
}
} catch (IOException e) {
String message = "Unable to calculate checksum";
if (resource != null) {
message += " for " + resource.getLocation() + " (" + resource.getLocationOnDisk() + ")";
}
throw new FlywayException(message, e);
}
return (int) crc32.getValue();
}
3) If you are using Flyway Pro version 5+, you can rollback the migration https://flywaydb.org/getstarted/undo.
The answers here are outdated but can still help you.
It sounds like you might be in one of two situations:
You want to re-run a versioned migration. This isn't really how flyway works, as Kartik has suggested, create a new versioned migration to alter the table.
A migration file has been modified and you want to leave it that way and run new ones (eg 0009.0). In this situation you can try:
Run repair. Which will recalculate the checksums (among other things).
Turn off the validateOnMigrate option which will not fail a migration if there are modified migration files.
To solve this error locally without dropping your whole db:
Fix the migration error which caused the root problem
Disconnect your db server
Open the table "flyway_schema_history" which would be created automatically
Delete the rows with the versions that are causing the mismatch problem
Open the tables that have columns depending on the conflict migrations and drop those columns (if needed)
Run again your db server with the new migrations

Azure Runbook for SQL procedure execution :Facing Timeout Issues

I have created a run-book in azure to automate my sproc execution on a scheduled interval, i see a timeout exception while the sproc gets executed.
I have tried changing the connection timeout value in my connection string to "0" to make it indefinite but I still see the same issue reoccurring. When I execute this stored procedure it hardly takes 3 minutes but the run-book fails with timeout exception.Can someone help me if I am missing anything.
Runbook type : PowerShell Workflow Runbook
Thanks,
Manoj.
Setting your connection string timeout to 0 will only affect the connection timeout, in other words you are giving your process infinite time to establish a connection with the database. Without seeing your code, I suspect the issue is with your command timeout - your stored proc is likely taking longer than the default 30s to complete. Before executing the stored proc you should set an appropriate timeout on the SqlCommand instance:
# Create command with a 10 minute timeout
$DatabaseCommand = New-Object System.Data.SqlClient.SqlCommand
$DatabaseCommand.Connection = $DatabaseConnection
$DatabaseCommand.CommandTimeout = 600
$DatabaseCommand.CommandText = "..."
# Execute non query
$NonQueryResult = $DatabaseCommand.ExecuteNonQuery()

Powercenter SQL1224N error connecting DB2

Im running a workflow in powercenter that is constatnly getting an SQL1224N error.
This process execute a query against one table (POLIZA) with 800k rows, it retrieves the first 10k rows and then it start to execute to another table with 75M rows, at ths moment in DB2 an idle thread error appear but the PWC process still running retrieving the 75M rows, when it is completed (after 20 minutes) the errros comes up related with the first table:
[IBM][CLI Driver] SQL1224N A database agent could not be started to service a request, or was terminated as a result of a database system shutdown or a force command. SQLSTATE=55032
sqlstate = 40003
[IBM][CLI Driver] SQL1224N A database agent could not be started to service a request, or was terminated as a result of a database system shutdown or a force command. SQLSTATE=55032
sqlstate = 40003
Database driver error...
Function Name : Fetch
SQL Stmt : SELECT POLIZA.BSPOL_BSCODCIA, POLIZA.BSPOL_BSRAMOCO
FROM POLIZA
WHERE
EXA01.POLIZA.BSPOL_IDEMPR='0015' for read only with ur
Native error code = -1224
DB2 Fatal Error].
I have a similar process runing against the same 2 tables and it is woking fine where the only difference I can see is that the DB2 user is different.
Any idea how can i fix this?
Regards
The common causes for -1224 are:
Your instance or database has crashed, or
Something/somebody is forcing off your application (FORCE APPLICATION or equivalent)
As for the crash, I think you would know by know. This typically requires a database or instance restart. At any rate, can you please have a look into your DIAGPATH to check for any FODC* directories whose timestamp would match the timestamp of the -1224 errors?
As for the FORCE case, you should find some evidence of the -1224 in db2diag.log. Try searching for the decimal -1224, but also for its hex representation (0xFFFFFB38).

How to detach a LocalDB (SQL Server Express) file in code

When using LocalDB .mdf files in deployment you will often want to move, delete or backup the database file.
It is paramount to detach this file first as simply deleting it will cause errors because LocalDB still keeps a registration of it.
So how is a LocalDB .mdf file detached in code?
I had to string together the answer from several places, so I wil post it here:
Mind, manually detaching the .mdf file from Visual Studio is possible after manually deleting it before detachment by going through SQL Server Object Explorer.
''' <summary>
''' Detach a database from LocalDB. This MUST be done prior to deleting it. It must also be done after a inadvertent (or ill advised) manual delete.
''' </summary>
''' <param name="dbName">The NAME of the database, not its filename.</param>
''' <remarks></remarks>
Private Sub DetachDatabase(dbName As String)
Try
'Close the connection to the database.
myViewModel.CloseDatabase()
'Connect to the MASTER database in order to excute the detach command on it.
Dim connectionString = String.Format("Data Source=(LocalDB)\v11.0;Initial Catalog=master;Integrated Security=True")
Using connection As New SqlConnection(connectionString)
connection.Open()
Dim cmd = connection.CreateCommand
'--Before the database file can be detached from code the workaround below has to be applied.
'http://web.archive.org/web/20130429051616/http://gunnalag.wordpress.com/2012/02/27/fix-cannot-detach-the-database-dbname-because-it-is-currently-in-use-microsoft-sql-server-error-3703
cmd.CommandText = String.Format("ALTER DATABASE [{0}] SET OFFLINE WITH ROLLBACK IMMEDIATE", dbName)
cmd.ExecuteNonQuery()
'--
'--Now detach
cmd.CommandText = String.Format("exec sp_detach_db '{0}'", dbName)
cmd.ExecuteNonQuery()
'--
End Using
Catch ex As Exception
'Do something meaningful here.
End Try
End Sub
I had the same issue and was thinking of how to deal with it.
There are 3 approaches.
Detach at the end of (or during) working with database
I didn't find the way to close connection in LinqToSQL, but actually it is not needed. Simply execute the following code:
var db = #"c:\blablabla\database1.mdf";
using (var master = new DataContext(#"Data Source=(LocalDB)\v11.0;Initial Catalog=master;Integrated Security=True"))
{
master.ExecuteCommand(#"ALTER DATABASE [{0}] SET OFFLINE WITH ROLLBACK IMMEDIATE", db);
master.ExecuteCommand(#"exec sp_detach_db '{0}'", db);
}
and make sure nothing will try to query db after (or you get it attached again).
Detach on start
Before you made any connection to db, detaching is as simple as:
var db = #"c:\blablabla\database1.mdf";
using (var master = new DataContext(#"Data Source=(LocalDB)\v11.0;Initial Catalog=master;Integrated Security=True"))
master.ExecuteCommand(#"exec sp_detach_db '{0}'", db);
This suit very well to my needs, because I do not care about delay to start application (because in this case I will have to attach to db always), but it will fix any kind of
System.Data.SqlClient.SqlException (0x80131904): Database 'c:\blablabla\database1.mdf' already exists. Choose a different database name.
which occurs, if database file is delete and you try to create it programmatically
// DataContext
if (!DatabaseExists())
CreateDatabase();
Another way
You can also run command line tool sqllocaldb like this:
var start = new ProcessStartInfo("sqllocaldb", "stop v11.0");
start.WindowStyle = ProcessWindowStyle.Hidden;
using (var stop = Process.Start(start))
stop.WaitForExit();
start.Arguments = "delete v11.0";
using (var delete = Process.Start(start))
delete.WaitForExit();
It will stop the server, detaching all databases. If you have other application using LocalDB, then they will experience attaching delay next time when they try to do query.
Here is my solution for EntityFramework Core 1.0
As you see the database name can be used with its full file path.
var dbf = fileDlg.FileName;
var options = new DbContextOptionsBuilder();
options.UseSqlServer($#"Server=(localdb)\mssqllocaldb;Initial Catalog=master;MultipleActiveResultSets=False;Integrated Security=True");
using (var master = new DbContext(options.Options))
{
master.Database.ExecuteSqlCommand($"ALTER DATABASE [{dbf}] SET OFFLINE WITH ROLLBACK IMMEDIATE");
master.Database.ExecuteSqlCommand($"exec sp_detach_db '{dbf}'");
}