This is a migrated thread and some comments may be shown as answers.

Exception: Attempted to read past the end of the stream

8 Answers 2275 Views
Data Access Free Edition
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Anthony
Top achievements
Rank 1
Anthony asked on 09 Oct 2014, 09:28 PM
I'm working on a project with the free version of Telerik Data Access and it works fine for a minute or two, but then it gives me this:

A first chance exception of type 'Telerik.OpenAccess.Exceptions.DataStoreException' occurred in mscorlib.dll
An exception of type 'Telerik.OpenAccess.Exceptions.DataStoreException' occurred in mscorlib.dll but was not handled in user code
Additional information: Attempted to read past the end of the stream.

I have many data context objects used throughout my entire project and it doesn't matter which one I try to use. After a while (about 60 seconds), any attempt to interact with the underlying database results in the exception shown above.

I also see this in the output window:

Telerik.OpenAccess Information: 95 : driver.con.createStat       7
Telerik.OpenAccess Information: 96 : driver.pool.badcon          Telerik.OpenAccess.RT.sql.SQLException: Attempted to read past the end of the stream. ---> System.IO.EndOfStreamException: Attempted to read past the end of the stream.
   at MySql.Data.MySqlClient.MySqlStream.ReadFully(Stream stream, Byte[] buffer, Int32 offset, Int32 count)
   at MySql.Data.MySqlClient.MySqlStream.LoadPacket()
   --- End of inner exception stack trace ---
   at Telerik.OpenAccess.RT.Adonet2Generic.Impl.PreparedStatementImp.executeQuery(Nullable`1 commandTimeout)
   at OpenAccessRuntime.Relational.conn.PooledPreparedStatement.executeQuery(Nullable`1 commandTimeout)
   at OpenAccessRuntime.Relational.conn.RelationalConnectionPool.validateConnectionImp(PooledConnection con)
Telerik.OpenAccess Information: 97 : driver.con.close            7
Telerik.OpenAccess Information: 98 : driver.con.createStat       8
Telerik.OpenAccess Information: 99 : driver.pool.badcon          Telerik.OpenAccess.RT.sql.SQLException: Attempted to read past the end of the stream. ---> System.IO.EndOfStreamException: Attempted to read past the end of the stream.
   at MySql.Data.MySqlClient.MySqlStream.ReadFully(Stream stream, Byte[] buffer, Int32 offset, Int32 count)
   at MySql.Data.MySqlClient.MySqlStream.LoadPacket()
   --- End of inner exception stack trace ---
   at Telerik.OpenAccess.RT.Adonet2Generic.Impl.PreparedStatementImp.executeQuery(Nullable`1 commandTimeout)
   at OpenAccessRuntime.Relational.conn.PooledPreparedStatement.executeQuery(Nullable`1 commandTimeout)
   at OpenAccessRuntime.Relational.conn.RelationalConnectionPool.validateConnectionImp(PooledConnection con)
Telerik.OpenAccess Information: 100 : driver.con.close            8


I have no idea what's wrong because my other Telerik based projects work fine. Any ideas of what I can try? I'm using the latest Data Access DLLs (2014.2.918.1).

These are the references in my project:

Telerik.OpenAccess
Telerik.OpenAccess.35.Extensions
Telerik.OpenAccess.40.Extensions

8 Answers, 1 is accepted

Sort by
0
Anthony
Top achievements
Rank 1
answered on 09 Oct 2014, 09:31 PM
I forgot to add the following information:

WPF/C#
The other developer doesn't run into this problem and isn't able to reproduce it.

I'm able to reproduce this problem every single time I run my program by simply waiting for a minute. It seems like the connection is timing out and then can never be successfully made again, but I have no idea why because even on my computer that only happens with this project.
0
Anthony
Top achievements
Rank 1
answered on 10 Oct 2014, 06:52 PM
Here are two examples of how I'm using the data context object:

public class ServiceProxy : IServiceProxy
{
    private PSEDataModel context;
     
    public Task<List<string>> GetCounties(string city)
    {
        return Task.Factory.StartNew(() =>
        {
            context = new PSEDataModel();
            return this.context.Populatedareas
                .Where(t => t.City == city)
                .Select(t => t.County)
                .Distinct()
                .OrderBy(t => t)
                .ToList();
        });
    }
      
    public Task<List<string>> GetStates()
    {
        return Task.Factory.StartNew(() =>
        {
            context = new PSEDataModel();
            return this.context.Populatedareas
                .Select(t => t.State)
                .Distinct()
                .OrderBy(t => t)
                .ToList();
        });
    }
}

This is my connection string in App.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="PSEDataModelConnectionMySQL" connectionString="server=68.178.143.46;user id=psetestdb;password=xxxxxxx;database=psetestdb" providerName="MySql.Data.MySqlClient"/>
  </connectionStrings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/></startup></configuration>


Could this be happening because the context object is being assigned new instances of the PSEDataModel object? How else should that kind of thing be done? Should I be created a new local instance in each Task?
0
Anthony
Top achievements
Rank 1
answered on 10 Oct 2014, 06:56 PM
Sorry, I keep forgetting to paste in relevant code. Here's how I call the Tasks from the above post. Note: some tasks are called simultaneously using await.

public List<string> Counties
{
    get { return _Counties; }
    set { _Counties = value; RaisePropertyChanged(() => this.Counties); }
}
 
private async void OnSelectedCityChanged(string obj)
{
    this.Counties = null;
    this.Counties = await this.serviceProxy.GetCounties(obj);
}
0
Anthony
Top achievements
Rank 1
answered on 11 Oct 2014, 01:34 PM
Here's a log of exactly when it happened right after a previously successful set of queries. It doesn't look any different to me than the previous queries...

01.Telerik.OpenAccess Information: 1197 : sm.begin                    90 optimistic
02.Telerik.OpenAccess Information: 1198 : sm.exec                     90 LINQ PSEDataModelMysql.Populatedarea Extent<PSEDataModelMysql.Populatedarea>().Select(t => t.State).Distinct().OrderBy(t => t) {Options:DC,ParallelFetch} fg:[default, -jdoreq]
03.Telerik.OpenAccess Information: 1199 : sm.fetch.batch              90 LINQ PSEDataModelMysql.Populatedarea Extent<PSEDataModelMysql.Populatedarea>().Select(t => t.State).Distinct().OrderBy(t => t) {Options:DC,ParallelFetch} fg:[default, -jdoreq]
04.Telerik.OpenAccess Information: 1200 : driver.pool.alloc           90 active=1/10 idle=6/10 con=-843794545
05.Telerik.OpenAccess Information: 1201 : driver.stat.execQuery       15 SELECT DISTINCT a.`State` AS COL1                  FROM `populatedareas` a ORDER BY a.`State` []
06.System.Windows.Media.Animation Warning: 6 : Unable to perform action because the specified Storyboard was never applied to this object for interactive control.; Action='Remove'; Storyboard='System.Windows.Media.Animation.Storyboard'; Storyboard.HashCode='64534707'; Storyboard.Type='System.Windows.Media.Animation.Storyboard'; TargetElement='System.Windows.Media.Animation.Storyboard'; TargetElement.HashCode='64534707'; TargetElement.Type='System.Windows.Media.Animation.Storyboard'
07.Telerik.OpenAccess Error: 1201 : MySql.Data.MySqlClient.MySqlException: Fatal error encountered during command execution.
08.CommandType=Text, CommandTimeout=30, ConnectionState=Closed, Transaction=
09.Telerik.OpenAccess Information: 1202 : driver.con.close            15
10.Telerik.OpenAccess Information: 1203 : driver.pool.release         90 active=0/10 idle=6/10 con=-843794545
11.Telerik.OpenAccess Error: 1199 : Attempted to read past the end of the stream.
12.A first chance exception of type 'Telerik.OpenAccess.Exceptions.DataStoreException' occurred in Telerik.OpenAccess.dll

From the above snippet, I noticed this specifically:

1.Telerik.OpenAccess Error: 1201 : MySql.Data.MySqlClient.MySqlException: Fatal error encountered during command execution.
2.CommandType=Text, CommandTimeout=30, ConnectionState=Closed, Transaction=

I don't understand why the connection state is closed or how to fix it. Maybe this isn't even the issue, but it seems wrong.
0
Anthony
Top achievements
Rank 1
answered on 11 Oct 2014, 08:27 PM
I fixed the memory leak from another post as follows:

public class ServiceProxy : IServiceProxy
{
    public Task<List<string>> GetCounties(string city)
    {
        return Task.Factory.StartNew(() =>
        {
            PSEDataModel context = new PSEDataModel();
            return this.context.Populatedareas
                .Where(t => t.City == city)
                .Select(t => t.County)
                .Distinct()
                .OrderBy(t => t)
                .ToList();
        });
    }
       
    public Task<List<string>> GetStates()
    {
        return Task.Factory.StartNew(() =>
        {
            PSEDataModel context = new PSEDataModel();
            return this.context.Populatedareas
                .Select(t => t.State)
                .Distinct()
                .OrderBy(t => t)
                .ToList();
        });
    }
}


The program crashes somewhat less frequently, but I'm still getting the same error about reading past the end of the stream. Something else is obviously wrong.

Sorry for all of the posts, but I figured documentation of what I've been trying would help the debug effort.
0
Boyan
Telerik team
answered on 14 Oct 2014, 01:45 PM
Hello Anthony,

Looking the latest code-snippet you provided us, I noticed this (please see the lines marked in red):
public Task<List<string>> GetCounties(string city)
{
    return Task.Factory.StartNew(() =>
    {
        PSEDataModel context = new PSEDataModel();
        return this.context.Populatedareas
            .Where(t => t.City == city)
            .Select(t => t.County)
            .Distinct()
            .OrderBy(t => t)
            .ToList();
    });
}

It seems that you use this.context rather then the context initialized on the row above. If this is indeed the case, it means that multiple threads are consuming the same connection at the same time which may in fact lead to some unexpected behavior.

The first step to resolving this issue would be to start using a context per task. Could you please try that let us know if it solves the issue on your side?

I am looking forward to hearing from you.


Regards,
Boyan
Telerik
 
OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
 
0
Anthony
Top achievements
Rank 1
answered on 14 Oct 2014, 04:58 PM
Thanks for your reply. The mistake you caught was actually a copy/paste error. My code is written as follows:

public Task<List<string>> GetStates()
{
    return Task.Factory.StartNew(() =>
    {
        PSEDataModel context = new PSEDataModel();
        return context.Populatedareas
            .Select(t => t.State)
            .Distinct()
            .OrderBy(t => t)
            .ToList();
    });
}
 
public Task<List<string>> GetCities(string state)
{
    return Task.Factory.StartNew(() =>
    {
        PSEDataModel context = new PSEDataModel();
        return context.Populatedareas
            .Where(t => t.State == state)
            .Select(t => t.City)
            .Distinct()
            .OrderBy(t => t)
            .ToList();
    });
}

However, I'm still facing the same issue.

Via the ticket system, I was told to look at this page to change the connection pool parameters. Specifically, I was told to use Pool.ADO, but I don't know what that means or how to use it. Also, when I copy the code into my project (I tried to use the config file method), the BackendConfiguration type isn't recognized and prevents compilation. That page doesn't actually show how to use the BackendConfiguration object or how to instantiate it.

It seems like the server is timing out and closing the connection after a minute, but the program is unable to transparently create a different connection.
0
Boyan
Telerik team
answered on 17 Oct 2014, 02:27 PM
Hi Anthony,

As explained in the mentioned thread, this is caused by broken connections. This could be result of some configurations on both your machine or on the database server ( for example, a limit of opened connections). As Telerik Data Access connection pool management handles connections in a different way, as a work-around you could try and see if switching to ADO connection pool will resolve the issue. Please refer to this documentation article for more detail on how Telerik Data Access manages the database connections.

The recommended approach to adjust the connection pool functionality via code is to extend your context class to a partial one. Here is an example (place this class in a new file):

public partial class EntitiesModel
{
    static partial void CustomizeBackendConfiguration(ref Telerik.OpenAccess.BackendConfiguration config)
    {
        config.ConnectionPool.Pool = Telerik.OpenAccess.ConnectionPoolType.ADO;
    }
}

Also, could you please us know the following:
1. Do you invoke stored procedures or functions in a similar way?
2. Could you please let us know your current connection pool options?
3. Do you manage the connection manually in any way?
4. Please make sure that you dispose each context each task.

I hope this is helpful. I am looking forward to your feedback.

Regards,
Boyan
Telerik
 
OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
 
Tags
Data Access Free Edition
Asked by
Anthony
Top achievements
Rank 1
Answers by
Anthony
Top achievements
Rank 1
Boyan
Telerik team
Share this question
or