Results 1 to 17 of 17

Thread: [RESOLVED] Linq2SQL statements appending issue with temp tables

  1. #1

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Resolved [RESOLVED] Linq2SQL statements appending issue with temp tables

    This is prob a little hard to demonstrate in an example ...

    but basically I am attaching extra statements to Linq2SQL database hits ...

    the issue I am having is this...

    I am creating a temp table in one fetch...

    VB.Net Code:
    1. Dim temp = dbml.Staff.Where(Function(x) x.FirstName.Contains("K")).IntoTempTable
    2. 'This actually gets created when this is called - so it creates one db hit...
    3. Dim asd = temp.Where(Function(x) x.FirstName.Contains("e")).ToArray()

    ... generates this sql:

    SQL Code:
    1. SELECT * INTO [#TempTable0] FROM
    2. (
    3.     SELECT  [tstaff0].[prodno] AS [Prodno], [tstaff0].[FirstName] AS [FirstName]
    4.     FROM [dbo].[staff] AS [tstaff0]
    5.     WHERE [tstaff0].[FirstName] LIKE CAST(N'%K%' AS VarChar(8000))
    6. ) AS TEMPTABLE
    7. SELECT [tstaff0].[prodno] AS [Prodno], [tstaff0].[FirstName] AS [FirstName]
    8. FROM [#TempTable0]
    9. WHERE ([tstaff0].[FirstName] LIKE '%e%')

    .. and a third...

    VB.Net Code:
    1. Dim asd2 = temp.Where(Function(x) x.FirstName.Contains("D")).ToArray()

    ... however this one errors as it tries to execute this sql:

    SQL Code:
    1. SELECT [tstaff0].[prodno] AS [Prodno], [tstaff0].[FirstName] AS [FirstName]
    2. FROM [#TempTable0]
    3. WHERE ([tstaff0].[FirstName] LIKE '%D%')

    ... and states: "Invalid object name '#TempTable0'"

    ... the issue is that the temp table is not kept after the first statement... for some reason... I do not believe that Linq2SQL performs reads in transactions that are rolled back... so what is going on?

    Thanks,
    Kris

  2. #2
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,474

    Re: Linq2SQL statements appending issue with temp tables

    Is there any other code between the first query on the temp table and the second?

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,348

    Re: Linq2SQL statements appending issue with temp tables

    There's something fishy about that code. In the first snippet, you declare 'temp' and 'asd' while in the second you declare 'asd', which suggests that they are in different scope, but you use 'temp' which suggests that they are in the same scope. What gives?

  4. #4
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,495

    Re: Linq2SQL statements appending issue with temp tables

    I assume (an I know this is bad) that you are using SQL Server is so temp tables that start with # are only available on a single connection to the database if you run a statement and close or leave the connection the temp table is no longer available. You could try ## (global temp table, not something I like) of create a true table in the database temp_TableName and then when completed drop the table.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  5. #5

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: Linq2SQL statements appending issue with temp tables

    Quote Originally Posted by jmcilhinney View Post
    There's something fishy about that code. In the first snippet, you declare 'temp' and 'asd' while in the second you declare 'asd', which suggests that they are in different scope, but you use 'temp' which suggests that they are in the same scope. What gives?
    Whops ... I was testing putting the 2nd statement as the first statement just to double check that it worked (which it did) and must have ended copying that...

    Quote Originally Posted by PlausiblyDamp View Post
    Is there any other code between the first query on the temp table and the second?
    No there isn't the code looks like this:
    VB.Net Code:
    1. Using dbml As New Database.DBML
    2.     Dim temp = dbml.Staff.Where(Function(x) x.FirstName.Contains("K")).IntoTempTable
    3.     Dim asd = temp.Where(Function(x) x.FirstName.Contains("e")).ToArray()
    4.     'The below line throws the error:
    5.     Dim asd2 = temp.Where(Function(x) x.FirstName.Contains("D")).ToArray()
    6.     'do stuff here
    7.     '...
    8. End Using

    Quote Originally Posted by GaryMazzone View Post
    I assume (an I know this is bad) that you are using SQL Server is so temp tables that start with # are only available on a single connection to the database if you run a statement and close or leave the connection the temp table is no longer available. You could try ## (global temp table, not something I like) of create a true table in the database temp_TableName and then when completed drop the table.
    I know this and for all I can tell it should be the same connection ... isn't that how Linq2SQL works (in the case of the above snippet)?

    ## is not really a good option for me.

    Kris

  6. #6

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: Linq2SQL statements appending issue with temp tables

    Ok... just ran an SQL profiler on it ...

    There appears to be:
    Code:
    Audit Login
    {stuff}
    Audit Logout
    For each query when running any L2SQL statement...
    So even if I go:
    VB.Net Code:
    1. Using dbml As New Database.DBML
    2.     Dim s1 = dbml.Staff.ToArray()
    3.     Dim s2 = dbml.Staff.ToArray()
    4. End Using
    I will get:
    Code:
    Audit Login
    {s1 stuff}
    Audit Logout
    
    Audit Login
    {s2 stuff}
    Audit Logout
    Is there anyway to stop L2SQL doing this?

    Thanks
    Kris

  7. #7

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: Linq2SQL statements appending issue with temp tables

    If I don't do it in a transaction scope (which I don't want to do anyway) it appears to get rid of the AuditLogins/Logouts between statements but strangely this still doesn't fix the issue???

    What is going on???

    Thanks,
    Kris

  8. #8
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,474

    Re: Linq2SQL statements appending issue with temp tables

    Is this running as a windows app or a web application? I am wondering if connection pooling is possibly getting involved, although I can't see why it would if you are executing the statements that close together.

    Is there anywhere that connection time outs or similar are being configured?

    Other than that I can't think of anything, and those ideas are clutching at straws really

  9. #9
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,495

    Re: Linq2SQL statements appending issue with temp tables

    You are stuck you can't control that. I agree as a DBA I don't like the global temp table. The only other suggestion is create a physical temp table (maybe in TempDB) do the work and drop at the end
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  10. #10

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: Linq2SQL statements appending issue with temp tables

    Quote Originally Posted by PlausiblyDamp View Post
    Is this running as a windows app or a web application? I am wondering if connection pooling is possibly getting involved, although I can't see why it would if you are executing the statements that close together.

    Is there anywhere that connection time outs or similar are being configured?

    Other than that I can't think of anything, and those ideas are clutching at straws really
    WinForms application ... not configuring anything that isn't defined in the connection string.

    Kris

  11. #11
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,474

    Re: Linq2SQL statements appending issue with temp tables

    Out of interest where is the .IntoTempTable method coming from? It has been a while since I have used Linq2Sql but I don't remember that being part of the standard set of functions?

    Do you have access to the source of the method?

  12. #12

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: Linq2SQL statements appending issue with temp tables

    Quote Originally Posted by PlausiblyDamp View Post
    Out of interest where is the .IntoTempTable method coming from? It has been a while since I have used Linq2Sql but I don't remember that being part of the standard set of functions?

    Do you have access to the source of the method?
    It's not ... It is my method... I do have the source code but I can't share it sorry...

    Hence why I said it would be "a little hard to demonstrate in an example"

    Kris

  13. #13
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,474

    Re: Linq2SQL statements appending issue with temp tables

    No problem, I completely understand.

    Given the rest of the code looks fairly normal linq I can only suggest investigating that method and seeing if it causes the problem, it could be down to some weird linq related issue.

  14. #14

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: Linq2SQL statements appending issue with temp tables

    Quote Originally Posted by PlausiblyDamp View Post
    No problem, I completely understand.

    Given the rest of the code looks fairly normal linq I can only suggest investigating that method and seeing if it causes the problem, it could be down to some weird linq related issue.
    It's code I am doing for my day job so I will ask my manager what I can post on here... Hopefully it will be enough to build up a test case and get some answers...

    Kris

  15. #15

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: Linq2SQL statements appending issue with temp tables

    I have finally gotten around to creating a test project...

    Which you can get here:
    Attachment 145443

    This will create a TestStuff database on .\SQLExpress ... If your SQL server instance is something else you can change it by editing the DataSource Const in Form1.

    Hit the "Start Test" button to test and watch it fail.

    Thanks in advance,
    Kris

  16. #16
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,474

    Re: Linq2SQL statements appending issue with temp tables

    Stepping through the code it looks like the code ends up calling the SqlProvider.Execute method (https://referencesource.microsoft.co...e9d902d6d122f6) as part of this it calls into SqlConnectionManger.UseConnection (https://referencesource.microsoft.co...9a6f21a01f4b82) which sets autoclose to true, I sort of made an educated guess that this is what resulted in the connection logging out...

    Digging through the DataContext source it seems that if you create an instance of context using a connection string then it will instantiate a connection object for you and will take care of opening and closing it for you.

    If you change how you use the TestStuff to
    Code:
     
    Using con As New SqlClient.SqlConnection(ConnectionString)
        con.Open()
        Using DBML = New TestStuff(con)
             Dim ExtraCommands As New List(Of String)
    and create it by passing in an open connection then you are responsible for also closing the connection, it looks like the DataContext leaves it alone, wrapping it in the Using block just makes this easier. Running the app now doesn't throw the exception, hope the stuff you see on screen is what you were expecting to see.

  17. #17

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: Linq2SQL statements appending issue with temp tables

    Quote Originally Posted by PlausiblyDamp View Post
    Stepping through the code it looks like the code ends up calling the SqlProvider.Execute method (https://referencesource.microsoft.co...e9d902d6d122f6) as part of this it calls into SqlConnectionManger.UseConnection (https://referencesource.microsoft.co...9a6f21a01f4b82) which sets autoclose to true, I sort of made an educated guess that this is what resulted in the connection logging out...

    Digging through the DataContext source it seems that if you create an instance of context using a connection string then it will instantiate a connection object for you and will take care of opening and closing it for you.

    If you change how you use the TestStuff to
    Code:
     
    Using con As New SqlClient.SqlConnection(ConnectionString)
        con.Open()
        Using DBML = New TestStuff(con)
             Dim ExtraCommands As New List(Of String)
    and create it by passing in an open connection then you are responsible for also closing the connection, it looks like the DataContext leaves it alone, wrapping it in the Using block just makes this easier. Running the app now doesn't throw the exception, hope the stuff you see on screen is what you were expecting to see.
    Thanks soooo much ... works like a treat

    Kris

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width