Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Victory Dan Greene · Dec 23, 2014 at 06:52 PM · sqliteasynchronousinterrupt

Can you interrupt a SQLite command? How?

I want to interrupt the execution of an IDbCommand. Does anyone know a way to force the command to cancel/abort/stop/cease and desist?

What I'm looking for is something along the lines of the core function sqlite3_interrupt() AKA “Interrupt A Long-Running Query”—but it doesn't seem Mono's SQLite library is designed for this. Is there a way to get SQLite working asynchronously like this?

Or, maybe, is there a working example anywhere of launching a SQLite command (in Unity) as an asynchronous process, then interrupting it? Google has come up empty.

Big thanks for any help you can provide!


History...

Using the command’s Cancel() method does nothing. Closing the command’s connection also does nothing.

Using BEGIN TRANSACTION and then calling a second command to ROLLBACK during execution throws out-of-memory errors and occasionally hangs the program.

It seems launching a SqliteCommand object asynchronously and then calling back to it like this is a no-no; Mono’s .chm docs specify:

“You May NOT call methods or properties or otherwise reference any SQLite provider classes that belong to another thread.”


Code...

This is the Start() event of a single MonoBehaviour attached to the main camera in an otherwise empty scene. Full script here (rename to .cs).

 // run once at startup
     public void Start() {
         Init() ;             // initialize database if it doesn't exist
         BeginFetch() ;       // launch a command 
         Thread.Sleep( 20 ) ; // wait a bit
 
         // interrupt with IDbTransaction.Rollback()
         // (throws "InvalidOperationException: Connection must be valid and open to commit transaction")
         //InterruptA() ;
 
         // interrupt by calling command's .Cancel() method and closing its connection
         // (doesn't do anything at all)
         //InterruptB() ;
 
         // interrupt by executing second command using commandText "ROLLBACK;"
         // (works, BUT sometimes crashes the program...)
         InterruptC() ;
 
     }
 
     // Sample output:
 
     // all Interrupt functions commented:
     //   Fetch() begins at 0 ms
     //   FetchCallback() begins at 194 ms
     //   Query completed in 194 ms: klaatu barada nikto
 
     // only InterruptA uncommented
     //   Fetch() begins at 1 ms
     //   InterruptA() begins at 20 ms
     //   InvalidOperationException: Connection must be valid and open to commit transaction
     //   FetchCallback() begins at 322 ms
     //   Query completed in 322 ms: klaatu barada nikto
 
     // only InterruptB uncommented
     //   Fetch() begins at 0 ms
     //   InterruptB() begins at 21 ms
     //   FetchCallback() begins at 275 ms
     //   Query completed in 275 ms: klaatu barada nikto
 
     // only InterruptC uncommented
     //   Fetch() begins at 0 ms
     //   InterruptC() begins at 21 ms
     //   Fetch interrupted: Mono.Data.SqliteClient.SqliteExecutionException: SQL logic error or missing database
     //   FetchCallback() begins at 29 ms
     //   Query completed in 30 ms: NULL


sqlitetest.txt (9.3 kB)
Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image f4bo · Dec 25, 2014 at 10:44 AM 1
Share

the usual way to cancel a sql is indeed to open a transaction and then rollback it so go ahead - don't bother too much about the "full memory" error right now, I guess depends on your test code that is a bit messy - I suggest you to remake it with just a clean single repeating query with rollback and see if the problem still happens.

avatar image Victory Dan Greene · Dec 29, 2014 at 03:06 PM 0
Share

I got rid of the code—turns out the way it worked is verboten anyway.

Rolling back a command (or undoing it) is the easy part! $$anonymous$$y goal is interrupting the command—that is, stopping its execution while it’s executing.

In this sense "Cancel" is the wrong word, so I'll update the question if I can to be more accurate.

avatar image f4bo · Dec 30, 2014 at 09:03 AM 0
Share

to help you dig into further I think it would be good you send us more details about your environment: Unity release, which sqlite plugin libraries, version etc you're using and also your test code as well could help a lot.

avatar image Victory Dan Greene · Dec 30, 2014 at 05:52 PM 0
Share

I'll add back the latest test code. As you mention, it is a bit messy, though refactoring doesn't help much.

Running in Unity 4.6.1f1 on OS X 10.8.5, using $$anonymous$$ono.Data.SqliteClient, setup detailed here.

avatar image f4bo · Dec 30, 2014 at 08:39 PM 0
Share

a while ago I downloaded a nifty asset store package named Sqliter and now I remember something in the comments talking about async usage with Unity, that I 'fetched' again and post here:

  /// - It is a flat file database.  You can change the settings to make it run completely in memory, which will make it even
 /// faster; however, you cannot have separate threads interact with it -ever-, so if you plan on using SQLite for any sort
 /// of multiplayer game and want different Unity instances to interact/read data... they absolutely cannot.

I'm not entirely sure if this is what you're trying to achieve but anyway what I'm seeing in your code looks indeed fiddling with multithreading, something Unity doesn't like much, for what I know. This is how much I can help you with this, sorry.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Victory Dan Greene · Dec 30, 2014 at 08:51 PM

It sure seems as though this can’t be done—for now, at any rate. Thanks for your help anyway!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Animatior Glitch when interrupted 0 Answers

Sqlite db error build iphone4 0 Answers

Clearing cached sqlite 1 Answer

Database (SQLite) Setup for Unity 0 Answers

Spatialite under Unity 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges