Oracle quartz selectWithLockSQL value? - java

I have this in my spring config for quartz....
<prop key="org.quartz.jobStore.selectWithLockSQL">SELECT *
FROM {0} LOCKS WITH (UPDLOCK ROWLOCK)
WHERE LOCK_NAME = ?</prop>`
That works fine for MS SQL Server, but I don't know what to use for Oracle.
Can anyone help me out please?

so what are you trying to do there? the syntax in Oracle to lock a row is:
SELECT * FROM <table_name> WHERE <condition> FOR UPDATE;

Related

How to get postgres schema from runinng query in Java

assume you do query on pg_stat_activity table and you get example result:
datid
datname
pid
usesysid
usename
application_name
client_addr
client_hostname
client_port
backend_start
xact_start
query_start
state_change
wait_event_type
wait_event
state
backend_xid
backend_xmin
query
backend_type
7198
10
rdsadmin
7195
16384
rdsadmin
32375
10
rdsadmin
PostgreSQL JDBC Driver
16409
c-t-s
21143
16410
c-t-s
c-t-s
10.10.3.1
48037
2021-01-18 13:19:03
2021-01-18 13:31:23
2021-01-18 13:31:23
Client
ClientRead
idle
COMMIT
client backend
I would like to know on which schema the query COMMIT was executed?
My case is i have schema-based multitenancy and i would like to distinguish between schemas (tenants). We always make a single-schema queries, so we dont mix them. To achieve that we set search_path on each getConnection method invocation. Code is developed in java and we dont use schema names in queries, as it is always dynamic -- taken from current request context and set in getConnection method.
With current result I dont know which tenant (schema) is causing slow / long queries.
I have tried to select from pg_class by ids taken from pg_stat_activity but without luck.
So far the comments did not answer my problem, is that possible at all?

Find Database size using Hibernate

I need to get the database size, I am using hibernate and Java. Used this query
("SELECT sum( data_length + index_length ) / 1024 / 1024 as DataBase_Size_in_MB FROM information_schema.TABLES where table_schema="+database)
This works fine in Mysql but hibernate gives information_schema.TABLES is not mapped error.
If you are using hibernate then the name after FROM hibernate tries to find out your POJO class where you would be giving mapping to DB tables.
If you want to use native SQL use use Session.createSQLQuery("");
I don't know exactly what you need but i think you could use DatabaseMetaData or you can use native query which i don't recommend

Why is Hibernate generating this SQL query?

I'm using Hibernate and a MySql server. I use multiple databases as "namespaces" (e.g. users, transactions, logging etc.).
So, I configued Hibernate to NOT connect to a particular database :
url = jdbc:mysql://127.0.0.1/
The databases where tables are located are defined in the hbm files through the catalog attribute :
<class name="com.myApp.entities.User" table="user" schema="" catalog="users"> ...
When I want to load some data, everything works fine and Hibernate seems to generate the expected SQL queries (by using the catalog prefix in the table names) e.g. :
select id from users.user
However, when I try to add a new record, Hibernate don't use the from [catalog].[table_name] syntax anymore. So I get a MySQL error 'No database selected'.
select max(id) from user
Hibernate is trying the get the future id to create a new record, but it doesn't specify in which database is located the table, it should be :
select max(id) from users.user
Why is Hibernate generating this invalid query ? Have someone ever experienced this problem ?
You need to specify the schema for the generator. See this question on SO for a more detailed answer.

Hibernate session.createQuery error while replace method with single quote

I got very typical issue. My dynamically generated query like this...
UPDATE Templates t SET t.TEMPLATE_DATA = replace(t.TEMPLATE_DATA, 'Test\'s Test', 'Kent"s Test'), t.TEMPLATE_DATA = replace(t.TEMPLATE_DATA, 'Test&quot;s&#32;Test', 'Kent"s Test'), UPDATE_DATE = NOW() where PRACTICE_ID = 1 AND CATEGORY_ID IN (1)
This works perfect when I explictily fire this query in db. but by using hibernate's session.createQuery(-- my query --) if thwows an error QueryTranslatorException.
Database : Mysql 5.3
Have any one faced this issue?
Thanks in advance.
Try to run this in Hibernate as native SQL query:
session.createSQLQuery(-- query text --);
Because if you use
session.createQuery(-- query text --);
Hibernate will try to execute it as HQL query which differs from usual SQL query.
HQL is object oriented query language. It operates in terms of objects rather then in terms of tables. Here posted a brief description of difference between SQL and HQL. But if you have time better to read appropriate sections of hibernate's documentation about HQL and Native SQL usage.
If you want to execute SQL Query in hibernate, Use : session.createSQLQuery(String query);

MyBatis executing multiple sql statements in one go, is that possible?

i was wondering if it is possible to execute multiple sql statements in 1 go.
For example the scenario that i want to delete rows from multiple tables, is there a way i can do things like..
<delete id="delete" parameterType="String">
DELETE FROM DUMMYTABLE_A where X=${value}
DELETE FROM DUMMYTABLE_B where X=${value}
</delete>
I'm using myBatis with Oracle. I guess there is something similar in other DB. Actually you always can create procedures in DB, which usually is better for the future, when you have to support the project.
<delete id="deleteUnfinishedData" parameterType="map">
{call
declare
begin
delete from TABLE1 where id = #{valueFromMap1};
delete from TABLE2 where id = #{valueFromMap2};
end
}
</delete>
Yes, most databases allow this. Usually you have to delimit your SQL statements with something. In PostGRES and MySQL it's a semicolon (;). In Microsoft SQL server you should use the keyword GO. [ May 2013 Update: As of SQL Server 2012, you can and should use semicolons to delimit your statements. After SQL Server 2012 (ie. the next version and beyond) these will be mandatory. Using GO is now the deprecated way to do things in SQL2012 and beyond). ]
MySQL / PostGRES example:
DELETE FROM DUMMYTABLE_A where X=${value};
DELETE FROM DUMMYTABLE_B where X=${value};
DELETE FROM DUMMYTABLE_C where X=${value};
MS-SQL example:
DELETE FROM DUMMYTABLE_A where X=${value}
GO
DELETE FROM DUMMYTABLE_B where X=${value}
GO
DELETE FROM DUMMYTABLE_C where X=${value}
Better databases (ie. not MySQL) will also support transactions with BEGIN TRAN / COMMIT TRAN / ROLLBACK TRAN. Using transactions you can actually batch all the statements into one atomic operation, where if part of it failed, all three would be rolled back. See http://www.sqlteam.com/article/introduction-to-transactions for some more information about those.
Most likely all you need is the semicolons between your SQL statements though!
if anyone got an error like
Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL
syntax; check the manual that corresponds to your MariaDB server
version for the right syntax to use near 'UPDATE mytable
You can fix this by allowing multi queries in your driver.
For mariadb it would be the same as MySQL
allowMultiQuery=true
described in following mybatis issue
https://github.com/mybatis/mybatis-3/issues/1497
This code works for multiple Select in one go in MSSQL:
<select id="selectMultipleQueries" resultSets="movies,genres" resultMap="multipleQueriesResult">
BEGIN
select M.ID_ as mId,
M.NAME_ as mName,
from TestMyBatis.dbo.Movie as M
where M.ID_ = #{id,jdbcType=INTEGER,mode=IN};
select G.ID_ as gId,
G.NAME_ as gName
from TestMyBatis.dbo.Genre as G
where G.ID_ = #{id,jdbcType=INTEGER,mode=IN};
END
</select>

Categories