Home » Eclipse Projects » Eclipse Scout » Multiple databases
Multiple databases [message #902247] |
Thu, 16 August 2012 10:06  |
Eclipse User |
|
|
|
Hi,
until now I always created an application that worked against one database. I now have a requirement that needs to pull in data from another database. I did a little test and created a second SQL service.
It seems that this works OK, at least for the queries I did. Although before I proceed with this, has anybody experience with this, and can anybody confirm that working with multiple databases works well, included transaction handling, roll back etc.
Regards Bertin
|
|
| | | | | |
Re: Multiple databases [message #950929 is a reply to message #902538] |
Sat, 20 October 2012 04:30   |
Eclipse User |
|
|
|
Hi to all,
I have the same requirement with Bertin, and because i am still getting knowing the Scout, i still using the convenient SQL class.
I think that the best solution would be to subclassing the SQL to use another ISqlService. Ufortunatelly the SQL class is final and uses everywhere the
ISqlService service = SERVICES.getService(usedServiceType);
I would recommend to rewrite it, but who am i to recommend. Anyway, i wrote an SQL2 class which is copy/paste of SQL, with four differences.
- It is not final.
- It has a protected constructor.
- It defines a getSqlService(), which can be extended.
- Every underlined method uses the getSqlService().
With that implementation someone could extend the SQL2 as following
class YourSql extends Sql2 {
private YourSql() {
}
public ISqlService getSqlService() {
return SERVICES.getService(YourSqlService.class);
}
}
and now can use the following syntax.
For anyone interesting i am uploading the code of SQL2. I would appreciate it if i had comments about other ways to implement it.
Attachment: SQL2.java
(Size: 5.40KB, Downloaded 334 times)
[Updated on: Sat, 20 October 2012 04:33] by Moderator
|
|
|
Re: Multiple databases [message #954059 is a reply to message #950929] |
Mon, 22 October 2012 15:07   |
Eclipse User |
|
|
|
To my mind, your code will not work.
SQL2.getSqlService() is a static method (public static), so you can not override this method in the child class (YourSql, the class that is extending SQL2).
SQL is a convenience class... It is suitable for one SQL Service (the default SQL Service. The one that will be returned by "SERVICES.getService(ISqlService.class)").
Do not expect to do more with this class. It is final, because it is not expected that someone create a subclass. If you have 2 services, you need to handle them separately.
ISqlService mainDb = SERVICES.getService(FirstSqlService.class);
mainDb.selectInto("select question_text from questions where question_id = :questionId into :questionText", formData);
ISqlService archiveDb = SERVICES.getService(SecondSqlService.class);
archiveDb.insert("insert facts(question_id, question_text, f1, f2, f3) values (:questionId, :questionText, :fact1, :fact2, :fact3", formData);
If you want convenience classes for your 2 SQL service, I recommend to write a class that will return the correct service:
public final class MainDb {
public static ISqlService get() {
return SERVICES.getService(FirstSqlService.class);
}
}
and:
public final class ArchiveDb {
public static ISqlService get() {
return SERVICES.getService(SecondSqlService.class);
}
}
In your code, it looks like you are applying the singleton pattern [with get() instead of getInstance()]
And you can use it:
MainDb.get().selectInto("select question_text from questions where question_id = :questionId into :questionText", formData);
ExternalDb.get().insert("insert facts(question_id, question_text, f1, f2, f3) values (:questionId, :questionText, :fact1, :fact2, :fact3", formData);
You also can also combine the 2 service accessors in one class:
public final class SQL2 {
public static ISqlService main() {
return SERVICES.getService(FirstSqlService.class);
}
public static ISqlService archive() {
return SERVICES.getService(SecondSqlService.class);
}
}
Your code will look like this:
SQL2.main().selectInto("select question_text from questions where question_id = :questionId into :questionText", formData);
SQL2.archive().insert("insert facts(question_id, question_text, f1, f2, f3) values (:questionId, :questionText, :fact1, :fact2, :fact3", formData);
I hope it helps.
|
|
| | | | |
Goto Forum:
Current Time: Wed Jul 23 14:13:23 EDT 2025
Powered by FUDForum. Page generated in 0.06968 seconds
|