473,386 Members | 1,943 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

ALL Java - MySQL rs.update at the same time error HELP...

jeffbroodwar
118 100+
Hi all !

I'm currently working on a project that involves webservices..... the problem is with my database, i can't make it process two rs.updates at the same time. it throws an sqlexception.... i can see that the problem is updating at the same time... how can i fix this? you see.... my program allows multiple users to insert data in mysql database.the first solution that i have thought is with the use of a file that saves all the queries.... then make the program execute these queries one by one.... in that way, even if we have 10 users inserting records at the same time, it'll just save it to a txt file then rs.update will be done one at a time... but i hope this problem can be solved by just adding few lines..... is there any technique to avoid this problem? i tried to use setautocommit(false) ,connection.commit(), to control concurrency but it still generates error.... i hope i can make this thing work..... even if it slows down when adding 100 records at the same time, the important thing is it doesn't miss even 1 record from that 100 rs.updates.... i hope you guys can help. please post some codes too.... i'm using netbeans 5.0,Mysql 5.0... any help will be much appreciated... thanx in advance ! ^^
Feb 26 '07 #1
2 2163
r035198x
13,262 8TB
Hi all !

I'm currently working on a project that involves webservices..... the problem is with my database, i can't make it process two rs.updates at the same time. it throws an sqlexception.... i can see that the problem is updating at the same time... how can i fix this? you see.... my program allows multiple users to insert data in mysql database.the first solution that i have thought is with the use of a file that saves all the queries.... then make the program execute these queries one by one.... in that way, even if we have 10 users inserting records at the same time, it'll just save it to a txt file then rs.update will be done one at a time... but i hope this problem can be solved by just adding few lines..... is there any technique to avoid this problem? i tried to use setautocommit(false) ,connection.commit(), to control concurrency but it still generates error.... i hope i can make this thing work..... even if it slows down when adding 100 records at the same time, the important thing is it doesn't miss even 1 record from that 100 rs.updates.... i hope you guys can help. please post some codes too.... i'm using netbeans 5.0,Mysql 5.0... any help will be much appreciated... thanx in advance ! ^^
It would have been better to see the code that's trying to do the updates at the same time or at least if you can tell us the specific sql exception that is being thrown. A simple solution is to use an arraylist to hold the sql statements as strings when they come and have a loop that retrieves the statements and executes them using one statement object
Feb 26 '07 #2
jeffbroodwar
118 100+
Here's the code...
Expand|Select|Wrap|Line Numbers
  1.  ------------------------------ 
  2. try
  3.  
  4. // CHECK IF CUSTOMER EXISTS THEN CHECK IF CUSTOMER ACCOUNT IS STILL ACTIVE //
  5. cmd = "Select * from customer where customerCode = " + SQuote(customerCode);
  6. rs = statement.executeQuery(cmd);
  7.  
  8. if (!rs.next()) // customer not found
  9. lastErrorNumber = -2;
  10. lastErrorMessage = "Customer not found";
  11. return -2;
  12. }
  13.  
  14.  
  15. // CHECK IF CUSTOMER IS STILL ACTIVE, MARK E IF CUSTOMER ACCOUNT IS EXPIRED THEN CHECK ALL TRANSACTION'S EXPIRY DATE UNDER THAT CUSTOMER CODE //
  16. cmd = "Select * from customer where expiryDate < " + SQuote(getDateTime()) + "and customerCode = " + SQuote(customerCode);
  17. rs = statement.executeQuery(cmd);
  18.  
  19. if (rs.next()) // customer account expired
  20. rs.updateString("status","E");
  21. rs.updateRow();
  22. lastErrorNumber = -6;
  23. lastErrorMessage = "Customer account expired";
  24. return -6;
  25. }
  26. // SELECT CUSTOMER IN CUSTOMER TABLE THEN PASS CUSTOMER CODE VALUE TO CUSTOMERCODE FIELD //
  27. cmd = "Select * from customer where customerCode = " + SQuote(customerCode);
  28. rs = statement.executeQuery(cmd);
  29.  
  30. // IF CUSTOMER FOUND RETURN CUSTOMER CODE PROCEED NEXT//
  31. rs.absolute(1);
  32. customerCode = rs.getString("customerCode");
  33.  
  34.  
  35. // CHECK IF PROGRAM EXIST THEN PROCEED NEXT //
  36. cmd = "Select * from program where programCode = " + SQuote(programCode);
  37. rs = statement.executeQuery(cmd);
  38.  
  39.  
  40. // IF PROGRAM NOT FOUND RETURN A VALUE //
  41. if (!rs.next()) 
  42. lastErrorNumber = -7;
  43. lastErrorMessage = "Program not found";
  44. return -7;
  45. }
  46.  
  47. // CHECK IF PROGRAM IS EXPIRED //
  48. rs.absolute(1);
  49. programStatus = rs.getString("status");
  50.  
  51. if (!programStatus.equalsIgnoreCase("A")) // program not active
  52. lastErrorNumber = -8; 
  53. lastErrorMessage = "program not active";
  54. return -8;
  55. }
  56.  
  57.  
  58. // IF PROGRAM FOUND RETURN PROGRAM CODE AND CONVERSION FACTOR PROCEED NEXT //
  59. programCode = rs.getString("programCode");
  60. conversionFactor = rs.getDouble("conversionFactor");
  61.  
  62.  
  63. // CHECK IF TERMINAL EXISTS THEN PROCEED NEXT //
  64. cmd = "Select * from terminal where terminalCode = " + SQuote(terminalCode);
  65. rs = statement.executeQuery(cmd); 
  66.  
  67. // PASS VALUE TO TRANSACTION DATE //
  68. transactionDate = getDateTime();
  69.  
  70.  
  71. // PASS EXPIRY DATE VALUE
  72. cmdDate = "Select " + SQuote(transactionDate) + " + 1 as newYear"; // process of getting new year value
  73.  
  74. rs = statement.executeQuery(cmdDate);
  75. rs.absolute(1);
  76. newExpiryYear = rs.getString("newYear");
  77.  
  78. cmdDate = "Select right(" + SQuote(transactionDate) + ",15) as newDate"; // process of getting new date value
  79.  
  80. rs = statement.executeQuery(cmdDate);
  81. rs.absolute(1);
  82. newExpiryDate = rs.getString("newDate");
  83.  
  84. expiryDate = newExpiryYear + newExpiryDate; // expiryDate = expiryYear(+1) + expiryDate
  85.  
  86.  
  87. // PASS VALUE FOR CLAIMED POINTS //
  88. claimedPoints = 0.00;
  89.  
  90. // IF PROGRAM CODE = BLANK THEN SET DEFAULT //
  91. if(programCode == "")
  92. {
  93. programCode = "DEFAULT";
  94.  
  95. // START COMPUTING FOR POINTS //
  96. totalPoints = transactionAmount / conversionFactor;
  97. remainingPoints = totalPoints;
  98.  
  99.  
  100. // INSERT AND INCREMENT COUNTER VALUE FOR REGISTER CUSTOMER TABLE //
  101. cmd = "Select * from counter where counterCode = 'transaction'";
  102. rs = statement.executeQuery(cmd);
  103.  
  104. rs.absolute(1);
  105. transactionId = rs.getInt("counterValue") + 1;
  106. rs.updateInt("counterValue",transactionId);
  107. rs.updateRow();
  108. connection.commit();
  109.  
  110. // INPUT TRANSACTION RECORD // 
  111. try 
  112. {
  113. // ADD NEW TRANSACTION RECORD //
  114. cmd = "SELECT * FROM transaction";
  115. rs = statement.executeQuery(cmd);
  116.  
  117. rs.moveToInsertRow();
  118. rs.updateInt("transactionId", transactionId);
  119. rs.updateString("customerCode", customerCode);
  120. rs.updateString("programCode", programCode);
  121. rs.updateString("transactionDate", transactionDate);
  122. rs.updateDouble("transactionAmount", transactionAmount);
  123. rs.updateDouble("conversionFactor", conversionFactor);
  124. rs.updateDouble("transactionQuantity", transactionQuantity);
  125. rs.updateDouble("totalPoints", totalPoints);
  126. rs.updateString("expiryDate", expiryDate);
  127. rs.updateDouble("remainingPoints", remainingPoints);
  128. rs.updateString("status", status);
  129. rs.updateString("terminalCode", terminalCode);
  130. rs.updateString("referenceCode", referenceCode);
  131. rs.updateString("transactionRemark", transactionRemark);
  132. rs.updateString("remark", remark);
  133. rs.updateString("customerCode", customerCode);
  134. rs.updateDouble("claimedPoints", claimedPoints);
  135. rs.insertRow();
  136. rs.first();
  137. connection.commit();
  138. }
  139.  
  140. // IF ERROR OCCURS CATCH HERE. EX: SQL ERROR FROM TRY BLOCK ABOVE //
  141. catch(SQLException e1) 
  142. {
  143. lastErrorNumber = -30;
  144. lastErrorMessage = "Database error occured : " + e1.getMessage();
  145. return lastErrorNumber;
  146. // IF ERROR ABOUT INVALID INPUT (NUMBERS) CATCH HERE. EX: INVALID TRANSACTION AMOUNT/QUANTITY VALUE //
  147. catch(NumberFormatException e3)
  148. {
  149. lastErrorNumber = -31;
  150. lastErrorMessage = "Invalid value for transaction amount / transaction quantity";
  151. return lastErrorNumber;
  152. }
  153.  
  154.  
  155. // IF ERROR OCCURS CATCH HERE. EX: OTHER EX: DATABASE CONNECTION PROBLEM //
  156. catch(Exception e2)
  157. {
  158. lastErrorNumber = -1;
  159. lastErrorMessage = "Error1: " + e2.getMessage();
  160. return lastErrorNumber;
  161. }
  162.  
  163.  
  164. return 0; // No error code
  165. }
  166.  
  167.  
  168. // IF ERROR OCCURS CATCH HERE. EX: SQL ERROR FROM TRY BLOCK ABOVE //
  169. catch(SQLException e1) 
  170. {
  171. lastErrorNumber = -30;
  172. lastErrorMessage = "Database error occured : " + e1.getMessage();
  173. return lastErrorNumber;
  174. }
  175.  
  176. // IF OTHER ERROR OCCURS, CATCH HERE. EX: DATABASE CONNECTION PROBLEM //
  177. catch(Exception e2)
  178. {
  179. lastErrorNumber = -1;
  180. lastErrorMessage = "Error2: " + e2.getMessage();
  181. return lastErrorNumber;
  182. }
  183. --------------------
  184.  
sorry i can't paste the whole code, i've omitted some database queriess that will make the process slower. the code posted will show you an idea of how this program works...to start this explanation. my program is a reward server, the process starts when a customer purchases from the pos... this pos will then give the webservice the needed parameters: customercode,programcode,etc.(check the rs.update codes...) my boss wants me to initialize a connection once and leave it open.... i guess if i'll do an open - close database connection it'll avoid this problem that i'm facing. anyway, he wants me to find away to avoid this errors using his prefered way. the service returns both -1 and -30 whenever i try to send information to the server at the same time. -30 + getMessage(None); -1 + getMessage(Null);..... i can't see the specific message that i need... but my assumption is that it has a problem with the same time update... so i tried to put the connection.commit(); in this way all database updates will not be done at the same time, instead the memory will store all queries and will wait for the commit to happen. since the commit allows multiple records to be added in just one call i thought it'll solve the problem... after i added this connection.commit() i only receive the -1 error(Null). is there any other way to solve this? i mean do i lack some code that will trigger database update settings.... or do i have to try a newer version of Mysql driver? i have downloaded version 5.0 (latest) if this will not work.... i'm really lost... thanx for showing interest.. ^^
Feb 27 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
1
by: jlee | last post by:
I'm pretty much a newbie on mysql, and I need some help. I am running mysql Ver 12.22 Distrib 4.0.24, for portbld-freebsd5.4 (i386) on a server hosting an active website. The site's developer...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
3
by: Juan Antonio Villa | last post by:
Hello, I'm having a problem replicating a simple database using the binary log replication, here is the problem: When the master sends an update to the slave, an example update reads as follows:...
1
by: PowerLifter1450 | last post by:
I've been having a very rough time installinig mySQL on Linux. I have been following the instructions form here: http://www.hostlibrary.com/installing_apache_mysql_php_on_linux Everytime I get to...
8
by: ajos | last post by:
hi frnds, im trying to convert my servlets database configuration from ms access to mysql database.however im getting some error like no driver found exception. to verify this error ive...
1
by: banging | last post by:
Hi there, I have a question regarding locking of tables so that when two or more people try to write or update the mysql tables, it locks up. Basically I only want one person to write to the...
3
by: Ananthu | last post by:
Hi This is my codings in order to access mysql database from java. Codings: import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.