Using H2 safely on Android
I need to use JDBC and H2 (instead of sqlite) for an Android app. The app will be composed of multiple Activities and will target modern android tablets (Android 3+). The activities lifecycle on Android is tricky, and what i want to avoid is H2 being killed by the OS and risking a corrupt database. What is the best approach to be safe? Should I always open the database in the onResume() callback of an Activy and close it in the onPause() callback? It would have to be done asynchronously from the main thread, and syncrhonized so another activity from the same app doesn't try to open H2 while it's being closed. I think it would be safe, but i am afraid of performance issues when navigating from one activity to another (opening h2 can take 500ms to 1s on my tegra 2 tablet) Or should I manage H2 from an android Service that all my activities would bind to? The service would be stopped by android when no activities are bound to it. I am new to android and maybe i am overcomplicating things... I would appreciate advices from other developpers who have used h2 on android. -- You received this message because you are subscribed to the Google Groups "H2 Database" group. To view this discussion on the web visit https://groups.google.com/d/msg/h2-database/-/kOCFwG43R-QJ. To post to this group, send email to h2-database@googlegroups.com. To unsubscribe from this group, send email to h2-database+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/h2-database?hl=en.
Re: Using H2 safely on Android
Hi, The activities lifecycle on Android is tricky, and what i want to avoid is > H2 being killed by the OS and risking a corrupt database. > The database shouldn't get corrupt if the application is killed. It's still better if the application isn't killed however, because this will speed up database recovery. But I don't know too much about Android to help you I'm afraid. Regards, Thomas -- You received this message because you are subscribed to the Google Groups "H2 Database" group. To post to this group, send email to h2-database@googlegroups.com. To unsubscribe from this group, send email to h2-database+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/h2-database?hl=en.
Re: Using H2 safely on Android
I think this is a pain point. If you open the database and the OS kills your app, you won't get a corrupted database but the next time you open it, it will take much longer (I assume h2 needs to do some cleanup on the unclosed database). Does anyone know a way to avoid this behavior? On Thursday, January 26, 2012 10:22:07 PM UTC+1, Robert Lebel wrote: > > I need to use JDBC and H2 (instead of sqlite) for an Android app. > > The app will be composed of multiple Activities and will target modern > android tablets (Android 3+). > > The activities lifecycle on Android is tricky, and what i want to avoid is > H2 being killed by the OS and risking a corrupt database. > > What is the best approach to be safe? Should I always open the database in > the onResume() callback of an Activy and close it in the onPause() > callback? It would have to be done asynchronously from the main thread, and > syncrhonized so another activity from the same app doesn't try to open H2 > while it's being closed. I think it would be safe, but i am afraid of > performance issues when navigating from one activity to another (opening h2 > can take 500ms to 1s on my tegra 2 tablet) > > Or should I manage H2 from an android Service that all my activities would > bind to? The service would be stopped by android when no activities are > bound to it. > > I am new to android and maybe i am overcomplicating things... I would > appreciate advices from other developpers who have used h2 on android. > -- You received this message because you are subscribed to the Google Groups "H2 Database" group. To unsubscribe from this group and stop receiving emails from it, send an email to h2-database+unsubscr...@googlegroups.com. To post to this group, send email to h2-database@googlegroups.com. Visit this group at http://groups.google.com/group/h2-database?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Re: Using H2 safely on Android
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 You can minimise the chances of your process being killed by running a service that calls startForeground(). Android will only kill a process containing a foreground service as a last resort. https://developer.android.com/guide/components/processes-and-threads.html#Lifecycle https://developer.android.com/reference/android/app/Service.html#ProcessLifecycle The service's onLowMemory() method will be called when the system is low on memory, before killing any processes containing foreground services, so that might be a good place to close the database cleanly. https://developer.android.com/reference/android/app/Service.html#onLowMemory%28%29 However, running a service in the foreground isn't very polite to other apps, which will be killed to make room for your app if necessary. A more polite alternative would be to run a service that doesn't call startForeground(), and to close the database cleanly if either onLowMemory() or onDestroy() is called. I don't recommend closing the database from the activity's onPause() or onDestroy() methods, since those will be called whenever the user switches to another activity or rotates the screen. Cheers, Michael On 23/05/13 14:08, Bastian Schöpp wrote: > I think this is a pain point. If you open the database and the OS > kills your app, you won't get a corrupted database but the next > time you open it, it will take much longer (I assume h2 needs to do > some cleanup on the unclosed database). Does anyone know a way to > avoid this behavior? > > > On Thursday, January 26, 2012 10:22:07 PM UTC+1, Robert Lebel > wrote: > > I need to use JDBC and H2 (instead of sqlite) for an Android app. > > The app will be composed of multiple Activities and will target > modern android tablets (Android 3+). > > The activities lifecycle on Android is tricky, and what i want to > avoid is H2 being killed by the OS and risking a corrupt database. > > > What is the best approach to be safe? Should I always open the > database in the onResume() callback of an Activy and close it in > the onPause() callback? It would have to be done asynchronously > from the main thread, and syncrhonized so another activity from the > same app doesn't try to open H2 while it's being closed. I think it > would be safe, but i am afraid of performance issues when > navigating from one activity to another (opening h2 can take 500ms > to 1s on my tegra 2 tablet) > > Or should I manage H2 from an android Service that all my > activities would bind to? The service would be stopped by android > when no activities are bound to it. > > I am new to android and maybe i am overcomplicating things... I > would appreciate advices from other developpers who have used h2 > on android. > > -- You received this message because you are subscribed to the > Google Groups "H2 Database" group. To unsubscribe from this group > and stop receiving emails from it, send an email to > h2-database+unsubscr...@googlegroups.com. To post to this group, > send email to h2-database@googlegroups.com. Visit this group at > http://groups.google.com/group/h2-database?hl=en. For more options, > visit https://groups.google.com/groups/opt_out. > > -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.10 (GNU/Linux) iQEcBAEBAgAGBQJRnl5cAAoJEBEET9GfxSfMQiMH/1d1O2+MzeWBgUWaULRvU6ey prnTRuec2cnGZdZ/Vts9rAtXHpEuYcbUHcjvwdTTbzH8V3GjbnMiPYo57UTHitaG o7mtt7tV5zP4tC9DdPukscZjGBjKHt1UjQe2X8qu0kfxFri+jG3lH5Slu2w1RoFO MyJ4vGS5ybpeWXs+gH1VprFL8E58nZYeDR5EJAxTbHi/5dKYpkAvaheT0YDfV/dK a/9bYe6n9QrII9cbeIvP7prWDLH5ucL+2WGKoMTjQ5fi6bJYCwyrwxOcCyyAgVdf uNCdc/2s+bkywFTwnaqBQS5HuZGN7yPVOV74sAXGUtAI53nM4hMjorC+qgNd5Bg= =O2W+ -END PGP SIGNATURE- -- You received this message because you are subscribed to the Google Groups "H2 Database" group. To unsubscribe from this group and stop receiving emails from it, send an email to h2-database+unsubscr...@googlegroups.com. To post to this group, send email to h2-database@googlegroups.com. Visit this group at http://groups.google.com/group/h2-database?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Re: Using H2 safely on Android
You could try running a "CHECKPOINT SYNC" command from onPause(). At the very least, that would dramatically reduce the amount of work that H2 has to do during recovery. -- You received this message because you are subscribed to the Google Groups "H2 Database" group. To unsubscribe from this group and stop receiving emails from it, send an email to h2-database+unsubscr...@googlegroups.com. To post to this group, send email to h2-database@googlegroups.com. Visit this group at http://groups.google.com/group/h2-database?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Re: Using H2 safely on Android
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On 24/05/13 08:09, Noel Grandin wrote: > You could try running a "CHECKPOINT SYNC" command from onPause(). > > At the very least, that would dramatically reduce the amount of > work that H2 has to do during recovery. To improve durability I've been experimenting with WRITE_DELAY and CHECKPOINT SYNC, as recommended here: http://h2database.com/html/advanced.html#durability_problems Can you tell me whether it's useful to call CHECKPOINT SYNC after each commit if WRITE_DELAY is set to 0? Or conversely, is it useful to set WRITE_DELAY to 0 if I'm going to call CHECKPOINT SYNC after each commit? Thanks, Michael -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.10 (GNU/Linux) iQEcBAEBAgAGBQJRp6lBAAoJEBEET9GfxSfMK1oH/3EnIuZmGi8lKCcBNfKsI42X F48OuSgQc8UDkbj0Ez7VtH+zZccceBC5NR2XIWhFdW1O3RW69uOT/rmYDeFSaFt6 7cX041FKHgT7SUIcVYOy4r+k8ZACi9WZBYibskv4Q1g8l5cTPbQzDlCETpzxpn6b nEm3EUyhKWWKsul/pmHUbonRfME4B0oE6ULqBQX6qz8iATAL5VOWWyFxEhId+Zgi dEOI0bSX5JK/PjocTM3lOFUmKphTZ0Od4+Lrn+o7ZFmH5zVyOqw6FkUf8ew4vlUl ZGl3WsUPhhCLCcQRMfNCp4mjZFaas9g1kwdPb5XIkEiq7ZMUivHgIDNkRieLTGo= =zUsF -END PGP SIGNATURE- -- You received this message because you are subscribed to the Google Groups "H2 Database" group. To unsubscribe from this group and stop receiving emails from it, send an email to h2-database+unsubscr...@googlegroups.com. To post to this group, send email to h2-database@googlegroups.com. Visit this group at http://groups.google.com/group/h2-database?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Re: Using H2 safely on Android
I opted to manage H2 from a service. All my activities bind themselves to this service in activity.onResume(), unbind themselves in onPause(). When no activity have been bound to the service for 5 seconds, the service stop itself, stop h2 and do some other cleanup. So when my app goes in the background without beeing destroyed, h2 is stopped after 5 seconds. If the user restore my app in the foreground, h2 is resumed (i restart the service if needed). My app has been published, and It seems to be working fine. But I still think i may have missed a simpler solution. It would be nice if h2 had an official recipe for android. -- You received this message because you are subscribed to the Google Groups "H2 Database" group. To unsubscribe from this group and stop receiving emails from it, send an email to h2-database+unsubscr...@googlegroups.com. To post to this group, send email to h2-database@googlegroups.com. Visit this group at http://groups.google.com/group/h2-database?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
[h2] Re: Using H2 safely on Android
Hi Robert I know half a year went by since your post but as we're roughly going through a similar though-process regarding our app, I was wondering whether you'd be able/wish to update your latest feedback on your enacted strategy. Your insights would be most valuable to us. Many thanks Sunny. -- You received this message because you are subscribed to the Google Groups "H2 Database" group. To unsubscribe from this group and stop receiving emails from it, send an email to h2-database+unsubscr...@googlegroups.com. To post to this group, send email to h2-database@googlegroups.com. Visit this group at http://groups.google.com/group/h2-database. For more options, visit https://groups.google.com/groups/opt_out.