jaquel / dapper-dot-net

Automatically exported from code.google.com/p/dapper-dot-net
Other
0 stars 0 forks source link

Dapper sometimes does not ensure a connection is open #154

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

Using the code/DB below, I had to add a connection.Open(); line to prevent the 
pictured "Database is not open" exception.

Strangely, the Open() line is not needed on my home system.
Also strangely, PRIOR to the connection.Open() line, I can run a simpler dapper 
Query statement using the same query and get valid data with no error. Even if 
the simpler query runs, the more complex one still fails with the same error. 
Example simpler statement which works:
var sqlResults = connection.Query(sql.getData);

What is the expected output? What do you see instead?
Gathers data without the exception.

What version of the product are you using? On what operating system?
See below:

Please provide any additional information below.

Using System.Data.SQLite (x86/x64) 1.0.88.0 (NuGet)
Using Dapper 1.13 (NuGet)
Windows 7 x64/Visual Studio 2010/Microsoft MVC4 framework.

Despite the documentation, this question and it's response from Marc implies 
Dapper handles connection opening now:
http://stackoverflow.com/questions/12628983/why-doesnt-dapper-dot-net-open-and-c
lose-the-connection-itself

Connection string:
Data Source=|DataDirectory|datatables.sqlite;

---

SQL:

SELECT users.id,
    users.first_name,
    users.last_name,
    dept.id,
    dept.name,
    access.id,
    access.name,
    extra.id,
    extra.comments,
    extra.review
FROM users
LEFT OUTER JOIN user_dept ON users.id = user_dept.user_id
LEFT OUTER JOIN dept ON dept.id = user_dept.dept_id
LEFT OUTER JOIN user_access ON user_access.user_id = users.id
LEFT OUTER JOIN access ON access.id = user_access.access_id
LEFT OUTER JOIN user_extra ON user_extra.user_id = users.id
LEFT OUTER JOIN extra ON extra.id = user_extra.extra_id;

---

C#:

            var connectionString = ConfigurationManager.ConnectionStrings["sqlite"].ConnectionString;

            using(var connection = new SQLiteConnection(connectionString)) {
                //connection.Open();
                var lookup = new Dictionary<int, Users>();
                var sqlResults = connection.Query<Users, Dept, Access, Extra, Users>(
                    sql.getData, (u, d, a, e) => {

                        Users user;
                        if (!lookup.TryGetValue(u.id, out user)) {
                            lookup.Add(u.id, user = u);
                        }
                        if(user.access == null)
                        user.access = new List<Access>();
                        user.access.Add(a);
                        user.dept = d;
                        user.extra = e;
                        return user;
                    }
                ).Distinct();

                var response = new DataTablesServerResponse()
                    { id = -1, error = "", fieldErrors = new List<DataTablesEditorFieldError>() };
                response.aaData = sqlResults;
                return response;
            }

Original issue reported on code.google.com by charlesn...@gmail.com on 23 Sep 2013 at 9:16

Attachments:

GoogleCodeExporter commented 9 years ago
Confirmed this happens with any SQL, not just the SQL provided in "createdb.txt"

Original comment by charlesn...@gmail.com on 1 Oct 2013 at 4:20

GoogleCodeExporter commented 9 years ago
I've looked at the code for this, and this happens because simple query methods 
manage the connection state but the multimap methods do not.

Original comment by matt.ham...@gmail.com on 19 Nov 2013 at 3:21

GoogleCodeExporter commented 9 years ago
For completeness sake it looks like `Execute` queries *also* don't manage the 
connection state. 
http://stackoverflow.com/questions/21285341/why-does-dapper-open-for-query-but-n
ot-execute/21286354?noredirect=1#21286354

Original comment by cpf...@valetude.com on 22 Jan 2014 at 3:14

GoogleCodeExporter commented 9 years ago
This has been fixed for quite some time

Original comment by marc.gravell on 5 Aug 2014 at 3:46

GoogleCodeExporter commented 9 years ago
Thank you, Marc. Writing OSS can be thankless work, but I really, really enjoy 
Dapper and find it so elegantly designed that I'd use the word, "Beautiful."

Original comment by charlesn...@gmail.com on 5 Aug 2014 at 4:04