B4J Question jRDC2: Can the same instance of the Jserver connect to 2 seperate DataBases?

Cableguy

Expert
Licensed User
Longtime User
Hi jRDC gurus!

Can the same instance (I one single jRDC server) connect to 2 (or more) databases?
If so, can a sample code/ config be provided?
Or do I really need to run 1 server per database?
 

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
JRDC and specifically C3Po and the class SQL actually connects to an instance of mysql (in your case) and that instance can contain one or more databases.

If by databases you mean instances, then as far as I known i t would be better to create 2 connection pools. But both can be served by one jrdc .jar server.
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
JRDC and specifically C3Po and the class SQL actually connects to an instance of mysql (in your case) and that instance can contain one or more databases.

So in my case, I can have 2 or more databases, conecting to them with one single connection pool?
can you please, help me once again, just providing a sort of "proof of concept" kind of snippet...
I'm guessing the real work is made by the config.properties file, right? and of course, doubling the handlers...(?)
 
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
Cableguy!

It is actually easier than you think, and you can do it with one single instance.

1.- User (root for example) must have access to both databases.
2.- In order to call a database or the other, you can do it at a query level.

Like this example:

B4X:
SELECT
mydatabase1.tblUsers.UserID,
mydatabse2.tblUsers.UserID
FROM
mydatabase1.tblUsers
INNERJOIN mydatabase2.tblUsers ON mydatabase1.tblUsers.UserID = mydatabase2.tblUsers.UserID

OR simply queries like:

B4X:
SELECT * FROM database1.table1

B4X:
SELECT * FROM database2.table1

You dont have to change your config files.
 
Last edited:
Upvote 0

Harris

Expert
Licensed User
Longtime User
You dont have to change your config files.

JdbcUrl=jdbc:mysql://localhost/harris?characterEncoding=utf8

My config file above where harris is the database (config.properties)

So, you are saying I can connect to another DB (as long as I prefix it) without qualifying it in the config (as your example) - providing it has the same user rights ?

Really?
 
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
Yes, totally! it does not matter if you wrote the database, that helps you only to avoid writing the database name before a table.

i actually tested in my enviroment before posting it!

I used my production database and the mysql database to make some test and it worked.
it will work also for mariadb, mysql and sql server.
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
Yes, totally! it does not matter if you wrote the database, that helps you only to avoid writing the database name before a table.

i actually tested in my enviroment before posting it!

I used my production database and the mysql database to make some test and it worked.
it will work also for mariadb, mysql and sql server.
Well Hells Bells, - I did not know...

Now I can include other (db's) into the fray that are distinct from the original project - but may be useful (and used) from time to time without embedding.
Thanks!

(everyday, I learn, I learn...)

and I am not even up to jRDC yet, still stuck on original version 1 that has worked well to date... Must find time to update...
 
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
I am still not using JRDC, because all my projects are for desktop, i havent found the need.

And it is funny, i learned this just 2 weeks ago! and i was as surprised as you.
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
Perhaps Erel might chime in here with a vote of confidence, further instruction and guidance.
However, a simple like would mean so much.
The master knows so much yet has little time to impart his wisdom UNLESS confronted a specific tidbit that may be useful to us - that we may have not already known... (specific to B4X).

Thanks
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
Upvote 0
Top