Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can i have 2 concurrent connections to an Mysql database?

Status
Not open for further replies.

jasear

Programmer
Mar 9, 2003
4
GB
Hi guys,

I have a form, I pass certain values to another file which validates the data.

After validation the data is entered into a single table of a database. The problem is I have 2 different databases with exactly the same schema but different database and table names, the database are also located on a different server.

I need to enter the exact same data in both the databases. I tried initially entering the data into one of he tables and it worked fine, however when i try to enter it in both the databases it only works for the first one and nothing is populated in the second database.

Here is the code:


Class.forName("com.mysql.jdbc.Driver").newInstance();
String url="jdbc:mysql://***.***.***.205/aovoice";
Connection Conn=DriverManager.getConnection(url,"vmc","1794x");
Statement stmt = Conn.createStatement();
int rs = stmt.executeUpdate( "UPDATE users SET email = '"+request.getParameter("vemail")+"', fullname = '"+request.getParameter("firstname")+" "+request.getParameter("surname")+"' WHERE mailbox = '"+request.getParameter("mailbox")+"'");
stmt.close();
Conn.close();

Class.forName("com.mysql.jdbc.Driver").newInstance();
String url2="jdbc:mysql://***.***.***.209/alwayson";
Connection Conn2=DriverManager.getConnection(url2,"vmc","1794x");
Statement stmt2 = Conn2.createStatement();
int rs2 = stmt2.executeUpdate( "UPDATE voicemail SET email = '"+request.getParameter("vemail")+"', fullname = '"+request.getParameter("firstname")+" "+request.getParameter("surname")+"' WHERE mailbox = '"+request.getParameter("mailbox")+"'");
stmt2.close();
Conn2.close();



What am i doing wrong?


 
You should ask this question in the Java forum, as it's a standard Java question. This forum is just for JSP/Servlet.

Some hints:

1.- You don't need to load the driver twice
2.- What happens if you change the order?
3.- Do both connections work separately?
4.- What does executeUpdate return in each case?

Cheers,
Dian
 
There is nothing technically wrong with what you are doing, and it should work ... are you seeing any errors in the logs ?

However, on another note, what you are doing seems dangerous to me - I'm hoping you are just posting snippets of code. You are in effect it seems, synchronizing data between two databases. Now, usually I would advise that you use a proper database synchronization tool (ie like Oracle's replication product) - but I doubt that MySQL has something like that !

What happens if one of the statetments fails ? Are you rolling back BOTH transactions correctly ?

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
hmmm, now this is strange. When i change the order it makes no difference. If i take the first part out and leave only the second part in it it still doesnt update the .209 database.

No matter what i do it doesnt update it via the form. However if i replace the getparam fields in the code with real values and then run the page out of the form i.e. by accessing it directly then it does populate the database.
 
Print the values of the params you're getting on the form, they may be wrong.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top