Derby SQL Script Bulk Import - java

my goal is to import data into a derby database. The data was extracted from a MySQL instance in form of a SQL dump and I took care with a script that I only use the insert-statements and also that the escaping of the MySQL specific syntax is transformed to Derby correctly.
To Test, I use the derby-maven-plugin:
export MAVEN_OPTS=-Xmx2048m; mvn derby:run
In the first step, I create the schema in the derby instance (using DDLUtils from MySQL, this works fine).
Second, I try to import the data. I use ij on the command line with the following (shortend) script:
CONNECT 'jdbc:derby://localhost:1527/foodmart';
SET SCHEMA APP;
autocommit off;
INSERT INTO "ACCOUNT" VALUES (1000,NULL,'Assets','Asset','~',NULL), (2000,NULL,'Liabilities','Liability','~',NULL),(3000,5000,'Net Sales','Income','+',NULL),(3100,3000,'Gross Sales','Income','+','LookUpCube(\"[Sales]\",\"(Measures.[Store Sales],\"+time.currentmember.UniqueName+\",\"+ Store.currentmember.UniqueName+\")\")'),(3200,3000,'Cost of Goods Sold','Income','-',NULL),(4000,5000,'Total Expense','Expense','-',NULL),(4100,4000,'General & Administration','Expense','+',NULL),(4200,4000,'Information Systems','Expense','+',NULL),(4300,4000,'Marketing','Expense','+',NULL),(4400,4000,'Lease','Expense','+',NULL),(5000,NULL,'Net Income','Income','+',NULL);
...
commit;
As you can see, for every row of a table there is a bracket containing the sample data.
There are of course several more insert statments for other tables. After finishing some inserts correctly, the bulk importing process chokes at a really big dataset (> 1000 rows) due to following exception (from the derby-logs):
java.lang.StackOverflowError
at org.apache.derby.impl.sql.compile.SetOperatorNode.bindExpressions(Unknown Source)
at org.apache.derby.impl.sql.compile.UnionNode.bindExpressions(Unknown Source)
at org.apache.derby.impl.sql.compile.TableOperatorNode.bindExpressions(Unknown Source)
at org.apache.derby.impl.sql.compile.SetOperatorNode.bindExpressions(Unknown Source)
at org.apache.derby.impl.sql.compile.UnionNode.bindExpressions(Unknown Source)
at org.apache.derby.impl.sql.compile.TableOperatorNode.bindExpressions(Unknown Source)
at org.apache.derby.impl.sql.compile.SetOperatorNode.bindExpressions(Unknown Source)
at org.apache.derby.impl.sql.compile.UnionNode.bindExpressions(Unknown Source)
at org.apache.derby.impl.sql.compile.TableOperatorNode.bindExpressions(Unknown Source)
at org.apache.derby.impl.sql.compile.SetOperatorNode.bindExpressions(Unknown Source)
at org.apache.derby.impl.sql.compile.UnionNode.bindExpressions(Unknown Source)
at org.apache.derby.impl.sql.compile.TableOperatorNode.bindExpressions(Unknown Source)
at org.apache.derby.impl.sql.compile.SetOperatorNode.bindExpressions(Unknown Source)
at org.apache.derby.impl.sql.compile.UnionNode.bindExpressions(Unknown Source)
... and many lines more repeatingly because of the recursive nature...
Cleanup action completed
ij prints on the command line:
FEHLER XJ001: DERBY SQL error: SQLCODE: -1, SQLSTATE: XJ001, SQLERRMC: java.lang.StackOverflowError^T^TXJ001.U
Is this meaning, that derby is not capable using SQL-bulk-import scenarios?
Should I instead switching to an csv based import?
I assume from the stacktrace method calls that derby is not able to create the query plan.
Could it be possible, that for every 'data bracket' of an insert statement it will create internly an UNION statement, adding a big amount of overhead to each INSERT query.
So should I try to split my long INSERT-statements in many concise statements?
I don't have the time to look at the derby sources, so please help me!

I just used many Import statements and it worked.
It seems that for every data bracket of an INSERT statement Derby generates a method call on the stack, leeding for many brackets to a stackoverflow due to many recursive method calls.
However You just need to transform an INSERT statement from
Insert INTO VALUES (1,2,3), ..., (4,5,6);
To multiple statements:
Insert INTO VALUES (1,2,3);
...
Insert INTO VALUES (4,5,6);

Related

Error : java.sql.SQLException: JZ0P1: Unexpected result type

My code is simple
CallableStatement stmt = Conn.prepareCall ("{call Reconciliation (?)}");
stmt.setString(date);
PS.executeUpdate();
Am using Sybase (Adaptive Server Enterprise/15.7.0) and jconnect4 drivers if it is relevant to solution.
My procedure(Reconcliliation) is quite huge so I couldn't post it here but it does some updates to some 1 tables (Recon) after some comparison of data from another 2 tables (Deals1 and Deals2). It do not return any out parameters in procedure, it takes only 1 in parameter which is date.
When I run java code and run the procedure using callable statement it produces some updates data in table (Recon, count is 500) and the error I get after that is this :
java.sql.SQLException: JZ0P1: Unexpected result type.
at com.sybase.jdbc3.jdbc.ErrorMessage.raiseError(Unknown Source)
at com.sybase.jdbc3.jdbc.SybStatement.updateLoop(Unknown Source)
at com.sybase.jdbc3.jdbc.SybStatement.executeUpdate(Unknown Source)
at com.sybase.jdbc3.jdbc.SybCallableStatement.executeUpdate(Unknown
Source)
at DBConnection.ExecuteProc(DBConnection.java:88)
Am pretty sure there is no error in my procedure (Reconciliation) because when I run the same procedure in Aqua Data Studio with command exec Reconciliation '04-Dec-2016' it doesn't give any error and produces full update in tables (Recon, total count is 800).
There is no error in java as well because it is giving sql exception. If am not wrong there must be some problem in middle i.e jconnect drivers or something else.
Please help me with this, thanks in adv.
Old question, but I couldn't find any good answers to this question online when I had the same problem.
The big hint came from http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.dc39001.0700/html/prjdbc0700/CHDGJJIG.htm
JZ0P1 Unexpected result type.
Description: The database has returned a result that the statement cannot
return to the application, or that the application is not expecting at this
point. This generally indicates that the application is using JDBC incorrectly
to execute the query or stored procedure. If the JDBC application is connected
to an Open Server application, it may indicate an error in the Open Server
application that causes the Open Server to send unexpected sequences of results.
The stored procedure is returning tables. Don't do:
PS.executeUpdate();
But do this instead:
ResultSet rs = callableStatement.executeQuery();
For old jdbc drivers, I´ve noticed that
PS.executeUpdate();
raises this error while
PS.execute();
does not

NullPointerException while accessing HSQLDB database

I am accessing HSQLDB through A jdbc drive in my java program. The script file of the database is as follows.
CREATE SCHEMA PUBLIC AUTHORIZATION DBA
CREATE CACHED TABLE TBL1(ID CHAR(5),NAME VARCHAR(11),ZIP VARCHAR(11))
CREATE UNIQUE INDEX IDX_ID ON TBL1(ID)
CREATE CACHED TABLE TBL2(ID CHAR(5),STREET CHAR(5))
CREATE INDEX IDX_RS_ID ON TBL2(ID)
CREATE CACHED TABLE TBL3(ID VARCHAR(8) NOT NULL PRIMARY KEY,LON FLOAT,LAT FLOAT)
CREATE GLOBAL TEMPORARY MEMORY TABLE OVERLAY(ID CHAR(8),AROUTE CHAR(10),ZROUTE
CHAR(10),TYPE CHAR(8)) ON COMMIT PRESERVE ROWS
SET TABLE TBL1 INDEX'4490648 5643624 0'
SET TABLE TBL2 INDEX'529105280 529105280 0'
SET TABLE TBL3 INDEX'775814624 0'
CREATE VIEW OVERLAYSPAN (ID,STREET) AS SELECT DISTINCT ID, NAME FROM OVERLAY AS O, TBL2 as
R WHERE O.ID = R.ID
CREATE USER SA PASSWORD ""
GRANT DBA TO SA
SET WRITE_DELAY 10
In the java program, I am first removing all entries (1000 entries) from TBL1 and reinserting new entries (1500) in the same table. The program executes normally, but, when I am attempting to re-execute the same program, I am getting the following error.
java.sql.SQLException: error in script file line: 9 S1000 General error
java.lang.NullPointerException in statement [SET TABLE TBL2 INDEX'529105280 529105280 0']
Here is the fill stack trace from the program.
java.sql.SQLException: error in script file line: 9 S1000 General error java.lang.NullPointerException in statement [SET TABLE TBL2 INDEX'529105280 529105280 0']
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.jdbcConnection.<init>(Unknown Source)
at org.hsqldb.jdbcDriver.getConnection(Unknown Source)
at org.hsqldb.jdbcDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at HSQLTesting.main(HSQLTesting.java:98)
Where at line 98, the program is attempting to make a connection with the database.
Connection sqlconnect = DriverManager.getConnection("jdbc:hsqldb:file:C:/birdseyedb/birdseye", "SA", "");
Any idea what's going wrong here?
Thanks in advance.

HSQLDB INSERT Value in a Database

Hi i have a Problem while inserting data in my databases ...
String query = "INSERT INTO gutscheine (code, Wert) values ("+Code+","+Wert+")";
SQLConnection sql = new SQLConnection("Gutscheine.db");
PreparedStatement ps;
try {
ps = sql.con.prepareStatement(query);
int i = ps.executeUpdate();
if (i == -1) {
System.out.println("db error : " + query);
}
} catch (SQLException e) {
e.printStackTrace();
}
sql.CloseConnection(); // my own funktion
My Error:
java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: GUTSCHEINE
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCPreparedStatement.<init>(Unknown Source)
at org.hsqldb.jdbc.JDBCConnection.prepareStatement(Unknown Source)
at com.daniel.guthaben.Gutschein.<init>(Gutschein.java:33)
at com.daniel.guthaben.Main.main(Main.java:7)
Caused by: org.hsqldb.HsqlException: user lacks privilege or object not found: GUTSCHEINE
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.SchemaManager.getTable(Unknown Source)
at org.hsqldb.ParserDQL.readTableName(Unknown Source)
at org.hsqldb.ParserDQL.readRangeVariableForDataChange(Unknown Source)
at org.hsqldb.ParserDML.compileInsertStatement(Unknown Source)
at org.hsqldb.ParserCommand.compilePart(Unknown Source)
at org.hsqldb.ParserCommand.compileStatement(Unknown Source)
at org.hsqldb.Session.compileStatement(Unknown Source)
at org.hsqldb.StatementManager.compile(Unknown Source)
at org.hsqldb.Session.execute(Unknown Source)
... 4 more
Here i am trying adding context from the root user ... (on the left you can see the table exists)
http://i.stack.imgur.com/FS5ws.png
Sorry I was using diffrent databases ... :(
it's generally not advised to use string building for your query. String query = "INSERT INTO gutscheine (code, Wert) values (?, ?)"; and then running it through a safe query preparation is going to be better.
That said, the error sounds pretty obvious? user lacks privilege or object not found: GUTSCHEINE. Doesn't look like you're connecting as a particular user from your code, so I suspect you forgot to pass along your username and password when setting up the database connection. If that part's fine, verify Gutscheine.db exists.
The problem here is simple. When you are inserting string values, you need to enclose them in single quotes.
For example, "insert into table(col1, col2) values ('ABC','xyz');"
Have a look at the example provided in this link:
https://www.tutorialspoint.com/hsqldb/hsqldb_insert_query.htm
In your case, you need to enclose the values of Code and Wert in single quotes before you insert, or at the time of insert.
The following approach worked for me:
Code = "'" + Code + "'";
Wert = "'" + Wert + "'";
Use your insert query now, and it should work.
Even though hsql is quite forgiving and requires no infrastructure, you need to create the table upfront - e.g.
CREATE TABLE Gutscheine ( code varchar 20, Wert varchar 20 )
Also, you might want to read about SQL-Injection and use PreparedStatement. While you're at it, google for "little bobby tables"
I haven't run into privilege issues with hsql yet, but you might want to check how you open the connection, e.g. if you give a proper user account. You can actually check a proper one by looking at the hsql storage, which is clear text. It should contain the required user name - e.g. "SA"
Also, with that error message on google, several threads come up that suggest that hsql version upgrade might help, or that you ended up with a corrupt database after not executing "SHUTDOWN;" after creation of the database. Another one is the database name "null" in your .script file. Well, check them for yourself and try which solution matches your problem (the two linked threads point to several possible solutions):
http://sourceforge.net/p/hsqldb/mailman/message/28944633/
http://www.coderanch.com/t/460394/JDBC/databases/HSQLDB-user-lacks-privilege-object

Jasper Reports: Error executing SQL statement (ORA-00942): Table or view does not exist

I have a report called Analysis which is populated by this simple query:
SELECT * FROM TB_ANL analysis WHERE analysis.CD_ANL = $P{FILTER}
Only one element will be selected (CD_ANL is unique).
When I edit this query in iReports (version 4.7), the Edit Query window actually shows the columns of the table (so it recognizes the table) and I successfully compile the report.
When I try to run it in my server, it accuses a ORA-00942 and says:
net.sf.jasperreports.engine.JRException: Error executing SQL statement for : AnalysisTemplate
at net.sf.jasperreports.engine.query.JRJdbcQueryExecuter.createDatasource(JRJdbcQueryExecuter.java:229)
at net.sf.jasperreports.engine.fill.JRFillDataset.createQueryDatasource(JRFillDataset.java:758)
at net.sf.jasperreports.engine.fill.JRFillDataset.initDatasource(JRFillDataset.java:623)
at net.sf.jasperreports.engine.fill.JRBaseFiller.setParameters(JRBaseFiller.java:1160)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:802)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:746)
at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:58)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:417)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:247)
at gov.chesf.leilaovenda.nucleo.negocio.analise.RelatorioGerador.gerarRelatorio(RelatorioGerador.java:57)
at gov.chesf.leilaovenda.web.analise.analise.action.Index.gerarAnalise(Index.java:725)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254)
...
Caused by: java.sql.SQLException: ORA-00942: table or view does not exist
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:288)
at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:745)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:219)
at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:813)
at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:1049)
at oracle.jdbc.driver.T4CPreparedStatement.executeMaybeDescribe(T4CPreparedStatement.java:854)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1154)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3370)
at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3415)
at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
at net.sf.jasperreports.engine.query.JRJdbcQueryExecuter.createDatasource(JRJdbcQueryExecuter.java:222)
... 85 more
So, basically, the problem is: I can view the table in iReports and in SQLDeveloper but I can't fill the report with it because the table or view "Does not exist". What is wrong with it? Is there any other reason why Jasper wouldn't be able to see the table?
Things I've done before posting here:
I Granted the user that Jasper is using.
The table exists, yes.
I checked the connection in Jasper. It's the same that Hibernate is using.
Problem solved.
Here was the problem:
It had nothing to do with the iReport itself, but with the schema being used by the user that the connection was logging in as.
The table TB_ANL should be referenced as OWNER.TB_ANL and not just as TB_ANL.
Possible solutions to this:
1) Update the schema to contain TB_ANL
2) Create a Synonym for OWNER.TB_ANL as TB_ANL so everytime you reference the table it will look in OWNER's schema.

HSQLDB primary key violation error in JUnit tests

We have a testing framework using JUnit, OpenEJB, Eclipselink and HSQLDB. Everything has worked fine so far, and testing the service-tier is a breeze. Now however, we are running into problems when doing mass imports on a table (using the service-tier,entitymanager) or for example persisting entities to a list multiple times in a service method.
THIS IS THE WEIRD PART: Our tests seem to only break if tests are run on a fast enough workstation from the command line with Maven. When I run the tests through Eclipse IDE, everything is fine but sometimes, randomly, it also fails. We suspect it might have something to do with the speed the tests are run with, as weird as it sounds. The exception is simple enough because basically it tells us we are trying to add an entity with an already existing id. We have multiple times checked our test data and the hsqldb database. There are no pre-existing rows with id's we are trying to use. Still hsqldb throws the primary key exception at some point. From our logs we can see that the conflicting ID is not always the same, it might be 300015 or 300008.
We are at our wit's end here. Could it have something to do with HSQLDB's transactions or something else causing stale data?
We are using HSQLDB 2.2.8, Eclipselink 2.3.0 and OpenEJB 4.0.0-beta2.
The relation we are trying to add entities to is mapped as following:
#OneToMany(mappedBy = "invoice", cascade = CascadeType.PERSIST)
private List<InvoiceBalance> getInvoiceBalanceHistory() {
if (invoiceBalanceHistory == null) {
this.invoiceBalanceHistory = new ArrayList<InvoiceBalance>();
}
return invoiceBalanceHistory;
}
The root exception is:
Caused by: java.sql.SQLIntegrityConstraintViolationException: integrity constraint violation: unique constraint or index violation; SYS_PK_10492 table: INVOICEBALANCE
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCPreparedStatement.fetchResult(Unknown Source)
at org.hsqldb.jdbc.JDBCPreparedStatement.executeUpdate(Unknown Source)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeDirectNoSelect(DatabaseAccessor.java:831)
... 82 more
Caused by: org.hsqldb.HsqlException: integrity constraint violation: unique constraint or index violation; SYS_PK_10492 table: INVOICEBALANCE
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.Constraint.getException(Unknown Source)
at org.hsqldb.index.IndexAVLMemory.insert(Unknown Source)
at org.hsqldb.persist.RowStoreAVL.indexRow(Unknown Source)
at org.hsqldb.TransactionManager2PL.addInsertAction(Unknown Source)
at org.hsqldb.Session.addInsertAction(Unknown Source)
at org.hsqldb.Table.insertSingleRow(Unknown Source)
at org.hsqldb.StatementDML.insertSingleRow(Unknown Source)
at org.hsqldb.StatementInsert.getResult(Unknown Source)
at org.hsqldb.StatementDMQL.execute(Unknown Source)
at org.hsqldb.Session.executeCompiledStatement(Unknown Source)
at org.hsqldb.Session.execute(Unknown Source)
EDIT:
I changed the primary key generation strategy from GenerationType.AUTO (that seems to use the TABLE-strategy by default) to IDENTITY. After this, our mass persists seem to work without fail. I still don't know why HSQLDB goes "out of sync" with the TABLE-strategy. I wouldn't want to change our jpa entities just because our testing framework is buggy :)
It might be possible that your allocationSize is defining a bottleneck on relatively fast platforms or occasionally.
i.e.
When defaulted to GenerationType.AUTO which defaulyts to table EclipseLink will cache ID upto the allocated value. It will then look up generator to confirm its last allocated value.
If a lookup happened around the edge of the allocationSize before the next set of ID is cached, then you might run into a race condition where eclipse link allocates the last id in the cache twice before it updates the cache and tries to use both for insert and both inserts fail and are rolledback. If you can you should check to see if this happens around when your allocation cache should be incremented, but perhaps that kind of check might change the behaviour
Most likely, you are running out of memory while importing lots of rows into a MEMORY table.
You should increase the memory allocation or define this particular table as a CACHED table.
Update: CACHED tables can be used in persistent databases, not in all-in-memory databases:
CREATE CACHED TABLE mytable ...
or for an existing table:
SET TABLE mytable TYPE CACHED
UPDATE:
If this is not caused by OOM, as changing the generation strategy confirms, then it seems the generating strategy might not be incrementing the generated primary key value at some point. The identity strategy relies on the database to create the generated value, which works fine.
For integrity constraint violation: unique constraint or index violation
If you're a debugger freak, you can rebuild hsqldb in debug mode and set a breakpoint in org.hsqldb.index.IndexAVLMemory#insert at a line where variable compare has been assigned with a condition on the breakpoint compare == 0.
The faulty row (the duplicate one for instance) will be the one passed as argument.

Categories