JDBC statement fails to delete row in specific MySql table - java

I have a table, say example1 and I'm using a jdbc statement to delete one of its rows. I have tried various methods, from delete from example1 where id = 1 to statement.addbatch(sql) but it does not delete the row. If I execute the same sql statement in Toad for Mysql it's able to delete the row just fine.
Weird thing is that using jdbc I am able to delete rows from other tables just fine; it's just this one particular table giving me unexpected results.
There is nothing special about this table. It has a primary key and no constraints/foreign key relationships.
Also, this delete is a part of a transaction so auto-commit is set to false and once all records get updated/inserted/deleted then the commit is done. This does not seem to have any problem with any other table and all the updates/deletes/inserts are done just fine.
Permission-wise this table has same permission for the db user that any other table in the db.
Any ideas or pointers will be greatly appreciated!

Turning on general logging on the database or profiling in the JDBC driver would show you what's actually going to the database:
http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-configuration-properties.html
Enable profiling of queries for Connector/J by adding this to your connection string: profileSQL=true
General Logging documentation:
http://dev.mysql.com/doc/refman/5.1/en/query-log.html
There's also mk-query-digest for sniffing your network traffic and analyzing the results:
http://www.maatkit.org/doc/mk-query-digest.html

I have come across the same situation
I remember there was a defnitely mistake in the query
Try to execute the query in the mysql sqlyog or any GUI and check if it works, i am 100% sure it wont work
then correct the query and check it

Related

Oracle Database with Java JDBC Batch Inserts - insert child before parent

I have an Oracle (11) database with a table that has a self-referencing constraint.
I have some REALLY HORRIBLE java code, that has been happily running for a long time, that creates a BATCH of two inserts and runs them. This part of our UNIT TEST code and has been happily running for years.
The strange thing is that the insert of the CHILD is added to the batch before the insert of the PARENT.
I'd have thought that would never work. But it does
Well, it works on my original database, but not on a new "copy".
So my table is like this:
create table my_table (primary_key_id INT not null, related_id INT null);
alter table my_table add primary key (primary_key_id);
create index fk_my_table on my_table (related_id);
ALTER TABLE my_table ADD CONSTRAINT fkc_my_table FOREIGN KEY (related_id) REFERENCES my_table (primary_key_id);
The JAVA code is like this:
public void wossupDoc(Connection con) throws Exception {
String sql = "INSERT INTO my_table (primary_key_id, related_id) values (?,?)";
try(PreparedStatement ps = con.prepareStatement(sql)){
ps.setLong(1, 100);
ps.setLong(2, 101);
ps.addBatch();
ps.setLong(1, 101);
ps.setNull(2, Types.NUMERIC);
ps.addBatch();
ps.executeBatch();
}
(I created that table and put this java code into a unit test exactly like this so I could run it repeatedly against the different database)
So this code WORKS when run against DATABASE "A", but doesn't work when run against DATABASE "B".
So, that's the same code, the same JDBC drivers, but a different URL in the database connection properties... Yes it's always autocommit=false.
I've confirmed that, in the scenario where it works, there are two records in the database table.
The databases were created by two different DBAs a number of years apart, and they are nearly identical ... But are obviously different somehow.
Database versions:
Oracle Database 11g Release 11.2.0.3.0 - 64bit Production
Oracle Database 11g Release 11.2.0.4.0 - 64bit Production
(Yes I know those are REALLY OLD versions, I'm trying to migrate our code to an Oracle 19 database, and that's how I've discovered this scenario)
Does anyone know WHY this ever worked? Is there a database/connection parameter I could tweak to turn it on or off??
My true problem is that I'm wondering whether I've got any "batching" code in the main app that might try to do this, and so has been working accidentally for the past few years; and I won't know until we go live with the new database and the users start doing things...
As I investigate further, this looks like its a behavioural change in Oracle between 11.2.0.3.0 and 11.2.0.4.0. Checking more recent versions of Oracle the behaviour is consistent - the entries in the batch have to be valid based upon the sequence they are added to the batch. Looks like I have to accept this behaviour and fix the code/data that's failing. Makes sense, but more work for me.
Given 11.2 is so old a version of Oracle, no one else should really be getting similar problems. Thanks to everyone who looked at the question...

Does JDBC request working with Insert query type?

I am trying to run a insert statement which will help me to create user account for my following api calls.
I looked at JMeter JDBC request which has select/update/ect... i don't see insert query type.
does anyone know how can i run some insert query to create the user for my database?
thanks
L.
You should use Update Statement Query type also for inserts
Update Statement - use this for Inserts and Deletes as well
You may use also Prepared Update Statement type:
Prepared Update Statement - use this for Inserts and Deletes as well
In JDBC terms "insert" equivalent is update therefore you need to choose Update Statement from the "Query Type" dropdown:
You might also find How to Create Test Data in a Database with JMeter useful, it provides more insight and examples.

Insert one large amount of data in one transaction

I have struggled with architectural problem.
I have table in DB2 v.9.7 database in which I need to insert ~250000 rows, with 13 columns each, in a single transaction. I especially need that this data would inserted as one unit of work.
Simple insert into and executeBatch give me:
The transaction log for the database is full. SQL Code: -964, SQL State: 57011
I don't have rights to change the size of transaction log. So I need to resolve this problem on the developer's side.
My second thought was to use savepoint before all inserts then I found out that works only with current transaction so it doesn't help me.
Any ideas?
You want to perform a large insert as a single transaction, but don't have enough log space for such transaction and no permissions to increase it.
This means you need to break up your insert into multiple database transactions and manage higher level commit or rollback on the application side. There is not anything in the driver, either JDBC or CLI, to help with that, so you will have to write custom code to record all committed rows and manually delete them if you need to roll back.
Another alternative might be to use the LOAD command by means of the ADMIN_CMD() system stored procedure. LOAD requires less log space. However, for this to work you will need to write rows that you want to insert into a file on the database server or to a shared filesystem or drive accessible from the server.
Hi you can use export/load commands to export/import large tables, this should be very fast.The LOAD command should not be using the transaction log.You may have problem if your user have no privilege to write file on server filesystem.
call SYSPROC.ADMIN_CMD('EXPORT TO /export/location/file.txt OF DEL MODIFIED BY COLDEL0x09 DECPT, select * from some_table ' )
call SYSPROC.ADMIN_CMD('LOAD FROM /export/location/file.txt OF DEL MODIFIED BY COLDEL0x09 DECPT, KEEPBLANKS INSERT INTO other_table COPY NO');

How to update Rows of a JDBC Read Only ResultSet

I'm hitting a problem when trying to update a ResultSet.
I'm querying the database via JDBC, and getting back a resultset which is not CONCUR_UPDATABLE.
I need to replace the '_' into ' ' at the specified columns. How could I do that?
String value = derivedResult.getString(column).replace("_", " ");
derivedResult.updateString(column, value);
derivedResult.updateRow();
This works fine on Updatable, but what if it's ResultSet.CONCUR_READ_ONLY?
EDIT:
This will be a JDBC driver, which calls another JDBC Drivers, my problem is i need to replace the content of the ResultSets, even if it's forward only, or Read only. If I set scroll_insensitive and updatable, there isn't a problem, but there are JDBC drivers that works with forward only resultsets.
Solutions:
Should I try to move the results to an inmemory database and replace the contents there.
Should I implement the resultset which acts like all my other classes: Calls the underlying drivers function with modifications if needed.
I don't want to use the resulst afterward to make updates or inserts. Basically this will be done on select queries.
In my experience updating the result set is only possible for simple queries (select statements on a single table). However, depending on the database, this may change. I would first consult the database documentation.
Even if you create your own resultset which would be updatable, why do you think that the database data would change? It is highly probable (almost certain) that the update mechanism uses code that is not public and only exists in the resultset instance implementation type of the jdbc driver you use.
I hope the above makes sense.

Inserting values to two tables

I have two tables table1 and table2 when I inserting first insert become successfull and while at second got an exception. Then I want to remove the first table's value. How it can
done in JAVA and SQL.
Thanks in advance;
This is handled by the fact that the database is transactional. Disable autocommit on your JDBC connection, commit after the two statements are successfully executed, or rollback if any of them has failed, and the database will rollback (cancel) the insertion of both statements.
Read the JDBC tutorial about transactions.
What you need is to put the two insert statements inside a Database transaction, so that either the two statements completed successfully or rollback if one of them failed, depending on database engine you are using, for mysql see this, it might be something like:
START TRANSACTION;
Insert into table1 values("....") ;
Insert into table2 values("....");
COMMIT;
For SQL Server see This
While you don't commit your change, you always can do a rollback to cancel your transaction. So if you send your query with JAVA (I don't know your code) and that you get an Exception then you can send a rollback to the database.
Depending on the database you are using disable auto commit. Then perform your insert value's, if the second one fails execute a rollback. If you want more specific help you will have to provide additional information.

Categories