0

i got a problem when i'm closing an h2 connection(release database) and try to connect to another h2 database(debug database).

Snipplet to connect:

this.connection = new JdbcConnectionSource(connectionString);

Snipplet to disconnect:

this.connection.close();

ConnectionString:

jdbc:h2:file:/data/data/my.app/databases/myapp
jdbc:h2:file:/data/data/my.app/testdatabases/myapp

Then instead of using the "new" connection the old one is used. I'm using different directories for the databases. In both cases when i try to toggle between database1 and database2 the .lock and .trace files are not deleted in their directories.

I want exclude implmentation failures on my side. I'm using h2(current version) with ormlite on android 4.2.2.

1 Answers1

0

Then instead of using the "new" connection the old one is used.

If you are creating a new ConnectionSource then you will need to re-create all of your DAO classes and clear the DAO cache from within the DaoManager. Each of the DAO classes has a copy of the old connection source that it uses.

ConnectionSource connectionSource = new JdbcConnectionSource(connectionString);
DaoManager.clearCache();
Dao<Foo,Integer> fooDao = DaoManager.createDao(connectionSource, Foo.class);
...
Gray
  • 115,027
  • 24
  • 293
  • 354
  • Thanks! This helps but now after reconnectin a SQLException is thrown: "Session closed". It happens when i query first the "new connection". But the database and tables are created successfully. Any ideas? – user2379652 Jun 19 '13 at 12:29
  • Hrm. I just added the following test and it seems to work fine @user2379652. https://github.com/j256/ormlite-jdbc/blob/master/src/test/java/com/j256/ormlite/dao/DoubleDbOpenTest.java – Gray Jun 19 '13 at 13:51
  • Thx I also tested this with a smaller database. There it also works. But i think 18 Tables shouldnt be a problem. A crazy thing that the exception stil is thrown but it works. I could read and save data from the database. – user2379652 Jun 19 '13 at 14:03
  • Could it be that you are dealing with a existing foreign collection _after_ the database is switched? Foreign collections have a cached DAO that might try to access the closed session @user2379652. – Gray Jun 19 '13 at 14:05
  • Well I wrote ORMLite so I better know the limitations. :-) You should refresh any objects with foreign collections with the new DAO to remake the foreign collections. Best of luck @user2379652. – Gray Jun 19 '13 at 14:47
  • Sorry still not working, but I'll try it tomorrow again. Maybe its a mistake on my side. – user2379652 Jun 19 '13 at 16:29