how to manage connections to dynamically created databases - java

I need to manage connections to multiple databases in my web app. following are facts regarding the current implementation:
1- I use Tomcat
2- databases are created dynamically at runtime ( i am using mysql)
without a doubt, having a connection pool to manage database connections is optimal.
Since the databases are not known at the start of the application, it was not possible for me to set up datasources and make connection pools. (I could not find a way in Tomcat to make dynamic connection pool: a connection pool that is created at runtime).
my question is: what other options do I have to work efficiently with connections to multiple databases ? (I don't have experience to implement connection pools myself)
is there any library that can be used with tomcat and allow me to establish multiple connection pools to different databases at runtime ? if not what do you suggest that I do instead of connection pools ?
i am fairly new with this issue therefore please correct and guide me if I am messing up concepts.
Thank you in advance.

The MySQL JDBC driver allows omitting the database name from the connection URL as follows:
jdbc:mysql://localhost:3306
You only need to specify the database by Connection#setCatalog() or directly in the SQL queries. See also its reference documentation:
If the database is not specified, the connection will be made with no default database. In this case, you will need to either call the setCatalog() method on the Connection instance or fully specify table names using the database name (that is, SELECT dbname.tablename.colname FROM dbname.tablename...) in your SQL. Not specifying the database to use upon connection is generally only useful when building tools that work with multiple databases, such as GUI database managers.
This allows you for creating a single and reuseable connection pooled datasource in Tomcat. You'll perhaps only need to rewrite your connection manager and/or SQL queries.

There are enough connection pooling framework in the open. Proxool is definitely among the best. Its pretty flexible and easy to use.

Related

Use MySQL and SQLite simultaneously

I created a simple SQLite connection using Java.
Now I want to activate MySQL simultaneously as an option to store the data online.
Is there an easier way than rewriting every method to use the MySQL connection? E.g. create a connection object with both JDBC Drivers at once, so I have only one connection variable?
E.g. create a connection object with both JDBC Drivers at once, so I have only one connection variable?
Short answer: No. You cannot use a single JDBC Connection object to update two completely different database systems. You couldn't even use a single JDBC Connection object to update the same type of database (e.g., SQLite or MySQL) in two different places unless you had some sort of replication enabled between the databases themselves.
If you re-vamped your code to work with your own custom Objects (Classes) then you could conceivably code those classes to persist the data in SQLite and/or MySQL, but that would still require separate connections to each database.

Differences of connection pool, jdbc and jndi

I need to know if my understanding on the above is correct.
In a connection pool you set multiple connections with the use of java.sql.Datasource.
In jdbc we directly specify the connection url and oracle.jdbc.driver.OracleDriver and it's always one connection, where another request has to wait until the connection has finished processing.
And with JNDI it's similar to direct jdbc where we refer the jdbc setting via a name, so that we can specify the connection url and other setting in the application server and not have them bound to the application, right?
Well these are two different things.
JDBC is Java Database Connectivity API, while JNDI is Java Naming and Directory Interface API.
The main thing here is that in a JNDI directory you're actually storing a JDBC DataSource, so, you're simply using JDBC to obtain a Connection via JNDI lookup.
In short words: JDBC is Database realm, JNDI lets you store Objects in a virtual context (the Directory) that can be local, remote (implementation details usually don't matters).
You access this context via names, obtaining stored objects, is good to share things among different modules.
Application Servers usually have a JNDI Context for sharing global objects among different application, Connection Poolers happen to be one of the most clear example of why sharing via JNDI is good (define 1 connection pooler, share between several webapps).
ConnectionPool
JDBC
JNDI
I know all the acronyms are daunting when you first learned Java, but get used to it, spend a lot of time reading .. especially official resources from Java/Oracle

Modify JDBC code to target multiple database servers for Inesrt/Update/Deletions

Is there a way that I can use JDBC to target multiple databases when I execute statements (basic inserts, updates, deletes).
For example, assume both servers [200.200.200.1] and [200.200.200.2] have a database named MyDatabase, and the databases are exactly the same. I'd like to run "INSERT INTO TestTable VALUES(1, 2)" on both databases at the same time.
Note regarding JTA/XA:
We're developing a JTA/XA architecture to target multiple databases in the same transaction, but it won't be ready for some time. I'd like to use standard JDBC batch commands and have them hit multiple servers for now if its possible. I realize that it won't be transaction safe, I just wan't the commands to hit both servers for basic testing at the moment.
You need one connection per database. Once you have those, the standard auto commit/rollback calls will work.
You could try Spring; it already has transaction managers set up.
Even if you don't use Spring, all you have to do is get XA versions of the JDBC driver JARs in your CLASSPATH. Two phase commit will not work if you don't have them.
I'd wonder if replication using the database would not be a better idea. Why should the middle tier care about database clustering?
Best quick and dirty way for development is to use multiple database connections. They won't be in the same transaction since they are in different connection. I don't think this would be much of an issue if this is just for testing.
When your JTA/XA architecture is ready, just plug it into the already working code.

MongoDB Java Driver database connection pooling with Tomcat

According to the MongoDB Java driver documentation, database connection pooling is magically handled by the Mongo object.
Does this mean it is safe to create an instance of a singleton object which connects to the MongoDB database in a servlet that will run when Tomcat starts and not worry about configuring database connection pooling in Tomcat via the context.xml?
Is this the right way to think about it? Am I misunderstanding some basic concept of Tomcat / database connection pooling in general?
We've been using the Java drivers via the CFMongoDB project and we use it as you describe, but in a ColdFusion application rather then in Java. Same idea though: one object is created and we reuse it and that object maintains the one connection to the Mongo server.
You can create one Mongo Java instance and it will maintain an internal pool of connections (default size of 10) - to you it's hidden and you don't need to worry about it. The Mongo Java docs recommend this:
http://www.mongodb.org/display/DOCS/Java+Driver+Concurrency
We have it running in production now and there have been no issues. Multiple web request threads use the same Mongo instance and Mongo is quick enough to deal with this using it's internal pool (we're doing logging so it can write very fast!).
It is worth remembering to call close() on any instances that you are finished with - this will stop connections getting used up on the Mongo server over time:
http://api.mongodb.org/java/2.5-pre-/com/mongodb/Mongo.html#close()
So in summary, don't worry about configuring Tomcat.
Hope that helps!

Object Pooling managment algorithm

Anyone can help me to write an algorithm for object pooling. I am very new in J2EE and i need object pooling for database connection. So please help out.
Thanks
Database connection pooling is a tough problem and is difficult to get it right. However, you could use several open source solutions which offer Connection Pooling. You can consider using
C3P0 or Apache DBCP to get the connection pooling you desire.
In case you are working in an application server environment such as Glassfish, Weblogic or Jboss, connection pooling is provided by the application server itself. You need to create a datasource and enable pooling that you wish to have.
Rather than writing your own Object pool you should probably use an existing solution, e.g.
Commons Pool: http://commons.apache.org/pool (for object pools in general)
or
Commons DBCP: http://commons.apache.org/dbcp (for db connection pools)
All of the major Servlet and Java EE containers come with their own Connection pooling implementations, available through JNDI. Check the documentation with your container.
I am very new in J2EE and i need
object pooling for database
connection.
The whole point of the Java EE platform is to relief the developper form writing such infrastructure code, and focus on the business logic. Whether the platform succeeds at this or not, is another debate, but it's a least the vision.
Given that you are new to it, I would strongly suggest that you spend some time to get an better understanding of what the platform provide, and what's the vision behind it.
Connection pooling is just one thing, the platform provide specific ways to deal with configuration, security, deployment, monitoring, etc.
Regarding connection pooling specifically, see What is best approach for connection pooling?

Categories