I stack with next problem: my application which consist of Tomcat 8, Java 11, Spring 5, Atomikos 5, PostgreSQL 9.3.5 throw some strange errors. In Spring Repository I have method, which run native SQL query with temp tables creating and then perform some SELECTs. And at first method call I have next log output:
Hibernate: /* dynamic native SQL query */ DROP TABLE IF EXISTS all_character; CREATE TEMP TABLE all_character AS (...); DROP TABLE IF EXISTS character_last_cut;
Hibernate: /* dynamic native SQL query */ SELECT * FROM ( SELECT sort_num ...
Hibernate: /* dynamic native SQL query */ DROP TABLE IF EXISTS character_last_cut_ch_column;
15:52:45,284 ERROR XAResourceTransaction:66 - XA resource 'commonDataSource': prepare for XID 'XID: 3139322E3136382E302E3130322E746D313631373336343336353139373030303532:3139322E3136382E302E3130322E746D3532' raised -7: the XA resource has become unavailable
org.postgresql.xa.PGXAException: Error preparing transaction. prepare xid=XID: 3139322E3136382E302E3130322E746D313631373336343
...
Caused by: org.postgresql.util.PSQLException: ERROR: cannot PREPARE a transaction that has operated on temporary tables
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2553)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2285)
and it's right in accordance with "It is not currently allowed to PREPARE a transaction that has executed any operations involving temporary tables, ..." - https://www.postgresql.org/docs/9.3/sql-prepare-transaction.html
But at the next method calls all work fine without any errors. Why that error throwed only at first method call and not throwed by followed calls?
Related
We are facing a serious problem with H2 database, version 1.4.199 - server mode. The application data layer creates a table programmatically if not exists, for example:
CREATE TABLE IF NOT EXISTS mytable (...);
CREATE INDEX IF NOT EXISTS idx_mytable ON mytable(mycol);
and works fine for days, writing data into the above table. After restarting the service, at first connection attempt, the engine throws the error
org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "mytable" not found; SQL statement:
CREATE INDEX "PUBLIC"."IDX_MYTABLE" ON "PUBLIC"."MYTABLE"("MYCOL")
If we try recovering the database, the sql script does not contain the "mytable" anymore, so the data are definitevely lost! We have hundreds of installations of the software, but the error happens occasionally on some of them (10%).
Ping us H2 properties which you used.
"spring.jpa.hibernate.ddl-auto" should be "update".
My following native query is not working:
Query createNativeQuery = entityManager.createNativeQuery(
"select id from cscache where id=? for update ");
Environment Details:
Mysql 5.6
Jboss 5.1
JPA 1.0
Error:
2014-12-12 10:20:14,581 WARN [org.hibernate.util.JDBCExceptionReporter]
SQL Error: 1064, SQLState: 42000 (http-0.0.0.0-8543-3:)
2014-12-12 10:20:14,581 ERROR [org.hibernate.util.JDBCExceptionReporter]
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'limit 2' at line 1 (http-0.0.0.0-8543-3:)
For Update basically puts a lock on rows, to achieve the same using JPA you need to use Lock Modes. To set the lock, you can use EntityManager or TypeQuery and use LockModeType.PESSIMISTIC_WRITE.
Refer this article
I haven't actually done a for update in hibernate, but I believe you can (and should?) do it on the Session or query object. Why not let hibernate do it instead of executing a native query?
According to the documentation on locking (Chapter 5: Locking, you can set the lock mode on either the session or the query.
Cscache cscache = (Cscache )session.get( Cscache.class, id, LockOptions.UPGRADE );
Specifying the LockOptions should result in a SELECT ... FOR UPDATE being executed.
Not familiar with JPA specifically, but it appears you're not telling it what the value of ? is. Check out setParameter.
My problem seems to be very simple but I have hard time resolving it.
I want to alter an PostgreSQL sequence using Hibernate via native sql query (other solutions are also welcomed) using next code:
Query query = getSession()
.createSQLQuery("ALTER SEQUENCE users_id_seq RESTART WITH ?")
.setInteger(0, 1);
query.executeUpdate();
But I am getting this error:
Hibernate:
ALTER SEQUENCE users_id_seq RESTART WITH ?
hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 0, SQLState: 42601
hibernate.engine.jdbc.spi.SqlExceptionHelper - ERROR: syntax error at or near "$1"
Best I'm aware, you cannot prepare that statement.
If hibernate allows it, emulate the prepared statement, instead of sending it to the server. If not, sanitize the variable and issue the final statement directly.
Alternatively, wrap it in a function with dynamic SQL:
http://www.postgresql.org/docs/current/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN
I have analyzing an code of application implemented Java SE (Spring) and Oracle 11g. In XXXDaoImpl class there are many queries writte as
select * from PREFIX.TableName
I have created a schema in oracle 11g via Oracle SQL Developer and imported all tables successfully.
But when an application tries to call any procedure it call MYPREFIX extention lets say
select * from MYPREFIX.TableName
I would like to know how to change the MYPREFIX name in SQL Developer to get code and newly created schema similat each other in order to run the application properly.
I am currently having following error.
ERROR 30 May 2013 18:06:02,036 [ChangeDeleteMHandler] [ChangeDeleteMHandler]: Error during change/delete msisdn procedure call
org.springframework.jdbc.UncategorizedSQLException: CallableStatementCallback; uncategorized SQLException for SQL [{call TABLENAME.changeM()}]; SQL state [72000]; error code [4063]; ORA-04063: package body "PREFIX.TABLENAME" has errors
ORA-06508: PL/SQL: could not find program unit being called: "PREFIX.TABLENAME"
ORA-06512: at line 1
; nested exception is java.sql.SQLException: ORA-04063: package body "PREFIX.TABLENAME" has errors
ORA-06508: PL/SQL: could not find program unit being called: "PREFIX.TABLENAME"
ORA-06512: at line 1
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:83)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:969)
at org.springframework.jdbc.core.JdbcTemplate.call(JdbcTemplate.java:1003)
at org.springframework.jdbc.object.StoredProcedure.execute(StoredProcedure.java:125)
Chances are PREFIX and MYPREFIX as Oracle Schema users, so in your scenario your application is accessing tables under the PREFIX schema and you've created a new MYPREFIX schema. If that's the case, you could grant privileges to your schema by running under the PREFIX:
GRANT SELECT, INSERT, UPDATE, DELETE ON YOUR_TABLE
TO MYPREFIX;
I have a table named CUSTOMERS with the following columns :
CUSTOMER_ID (NUMBER), DAY(DATE), REGISTERED_TO(NUMBER)
There are more columns in the table but it is irrelevant to my question as only the above columns are defined together as the primary key
In our application we do a large amount of inserts into this table so we do not use MERGE but use the following statement :
INSERT INTO CUSTOMERS (CUSTOMER_ID , DAY, REGISTERED_TO)
SELECT ?, ?, ?
FROM DUAL WHERE NOT EXISTS
(SELECT NULL
FROM CUSTOMERS
WHERE CUSTOMER_ID = ?
AND DAY = ?
AND REGISTERED_TO = ?
)";
We use a PreparedStatement object using the batch feature to insert a large number of records collected through the flow of the application per customer.
Problem is that sometimes I get the following error :
ORA-00001: unique constraint (CUSTOMERS_PK)
violated
Strange thing is that when I do NOT use batch inserts and insert each record one by one (by simply executing pstmt.execute()) there are no errors.
Is it something wrong with the insert statement ? the jdbc driver ? Am I not using the batch mechanism correctly ?
Here is a semi-pseudo-code of my insertion loop :
pstmt = conn.prepareStatement(statement);
pstmt.setQueryTimeout(90);
for each customer :
- pstmt.setObject(1, customer id);
- pstmt.setObject(2, current day);
- pstmt.setObject(3, registered to);
- pstmt.addBatch();
end for
pstmt.executeBatch();
It is all enclosed in a try/catch/finally block making sure the statement and connection are closed at the end of this process.
I guess you are using several threads or processes in parallel, each doing inserts. In this case, Oracle's transaction isolation feature defeats your attempt to do the merge, because sometimes the following is bound to happen:
session A runs your statement, inserts a row (x,y,z)
session B runs the same statement, tries to insert row (x,y,z), gets a lock and waits
session A commits
session B receives the "unique constraint violated" error
That's because until session A commits, session B doesn't see the new row, so it tries to insert the same.