I have a Play Framework application, which uses Hibernate (5.2.12.Final) to connect to Postgresql (library version: 42.1.4, Postgresql 9.6). I added the following code to my application (I wanted to start using Hibernate Search, so I need session object):
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
session.flush();
The code hangs on the second line and application log is showing:
...
Hibernate: alter table creative_works_inner_tags drop constraint FK1m3t9vv4yl0o36k9nv0bjko87
Hibernate: alter table creative_works_inner_tags drop constraint FKcodcu7qrti27rqnv54bg9o0ma
Hibernate: alter table entry_headings drop constraint FK4tx66i2tsu651p4s176ea2nvk
So it looks like Hibernate hangs somewhere in the middle of schema update, which is triggered by session start. I also see a lock in Postresql:
select pid,
usename,
pg_blocking_pids(pid) as blocked_by,
query as blocked_query
from pg_stat_activity
where cardinality(pg_blocking_pids(pid)) > 0;
pid | usename | blocked_by | blocked_query
-----+---------+------------+------------------------------------------------------------------------
18804| pbl | {18499} | alter table entry_headings drop constraint FK4tx66i2tsu651p4s176ea2nvk
I don't know what to do about it. It also happens with previous library versions.
Update
The pid that blocks the ALTER TABLE command comes from the same application:
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
-------+---------+-------+----------+---------+------------------------+-------------+-----------------+-------------+------------------------------+-------------------------------+-------------------------------+-------------------------------+-----------------+------------+---------------------+-------------+--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
16388 | pbl | 18499 | 16387 | pbl | PostgreSQL JDBC Driver | 127.0.0.1 | | 51192 | 2018-01-12 10:23:11.62866+01 | 2018-01-12 10:24:11.363172+01 | 2018-01-12 10:24:11.574648+01 | 2018-01-12 10:24:11.574693+01 | | | idle in transaction | | 373556 | select children0_.parent_code as parent_c3_179_0_, children0_.code as code1_179_0_, children0_.code as code1_179_1_, children0_.ord as ord2_179_1_, children0_.parent_code as parent_c3_179_1_ from record_types children0_ where children0_.parent_code=$1
I don't trigger this SELECT explicitly in my code. It must be another Hibernate operation which happens automatically. Also, this code fragment is not explicitly parallel.
Thank you very much for the comments. They helped me to solve the problem.
The problem was: I was using both hibernate.cfg.xml (with session obtained from SessionFactory) and persistance.xml (with another session obtained from EntityManager). This was a result of integrating two applications into one and I think it caused wo Hibernate sessions to be created. I integrated necessary configuration from hibernate.cfg.xml into persistence.xml (had to change format) and it helped.
Related
I am wondering is it possible to select only the default value of empty column?
I have absolutely empty table and I want just to select one of the columns default value - it is important for my JAVA app which is filling the table.
Thanks.
You can get the default from the INFORMATION_SCHEMA.COLUMNS
select COLUMN_DEFAULT
from INFORMATION_SCHEMA.COLUMNS
where TABLE_SCHEMA='your_db' and TABLE_NAME='your_table' and COLUMN_NAME='your_column'
You can define a default value for a column when you create a table, if you just want MySQL to insert it automatically:
create table my_table (i INT DEFAULT 1);
But if you mean you want the default value which is stored in the DB dictionary, you can get it by this query:
SELECT Column_Default
FROM Information_Schema.Columns
WHERE Table_Schema = 'yourSchema'
AND Table_Name = 'yourTableName'
AND Column_Name = 'yourColumnName'
I can only think of two ways:
Inserting a row
Insert a row without specifying a value for that column
Select the column from that row; it will have the default value of the column
Delete the row
...probably all in a transaction so nothing else sees it.
Using describe (explain)
The describe command (aka explain) describes objects in the system, including tables. So if you do explain YourTable, you'll get back information about the table, including its default values.
Here's an example from that linked documentation:
mysql> DESCRIBE City;
+−−−−−−−−−−−−+−−−−−−−−−−+−−−−−−+−−−−−+−−−−−−−−−+−−−−−−−−−−−−−−−−+
| Field | Type | Null | Key | Default | Extra |
+−−−−−−−−−−−−+−−−−−−−−−−+−−−−−−+−−−−−+−−−−−−−−−+−−−−−−−−−−−−−−−−+
| Id | int(11) | NO | PRI | NULL | auto_increment |
| Name | char(35) | NO | | | |
| Country | char(3) | NO | UNI | | |
| District | char(20) | YES | MUL | | |
| Population | int(11) | NO | | 0 | |
+−−−−−−−−−−−−+−−−−−−−−−−+−−−−−−+−−−−−+−−−−−−−−−+−−−−−−−−−−−−−−−−+
So you can extract the default from the Default column in the returned rows.
Ah, of course, there's a third way, see slaakso's answer for it.
We're building a Java web application. We're using EJB containers and JPA with Wildfly 9.2.
Now we want to integrate a permission system in which a user has a certain role, but this role is only granted in combination with a certain entity. I'll name this access resource Department.
So we would have a list of permission for the user stored in such a table:
| User ID | Department | Role |
| ------- | ---------- | ------- |
| 1 | A | MANAGER |
| 1 | B | ADMIN |
| 2 | B | MANAGER |
In addition we have global roles. A user has a set of global roles that will be applied should there not be an entry in the table above with the deparment in question.
| User ID | Role |
| ------- | ------- |
| 1 | VIEWER |
| 3 | MANAGER |
How can we easily check if a user is in a certain role, given a department?
By just using the annotation #RolesAllowed we can check for a certain role, but not restricted to a department.
Remember that enterprise java security roles are about authorization.
So, you could model the department as a role in your security system as well. It's quite OK for a user to be assigned more than one security role.
Therefore, given a manager in department B:
#RolesAllowed({"manager", "B"}
public void someBusinessMethod(...) {
...
}
Only users who are managers and in department B are authorized to invoke this method.
Turns out EJB does not have the features we wanted.
We migrated to Apache Shiro Security and are super happy with it.
Resource based permissions always have to be checked programmatically. Static role and permission based checks can be annotations.
I need to copy data from one data table to another as mentioned below
When I enter company manager details of:
|NAME |VALUE |
|title |$randomList |
|firstName |$random |
|initial |$random |
|surname |$random |
When I enter company employee details of
|NAME |VALUE |
|title |$randomList |
|firstName | |
|initial | |
|surname | |
I am new to jbehave. In the above tables when i execute this scenario random characters will be generated and assigned to firstname,initial,surname in the 1st table I somehow need the same data when I execute the second table. I know we should parametrize by using examples table but in this case I don't want to use it. so how to copy the data from one table to another.
I currently got a performance issue with an application that uses many sql select.
The programming language is java and I'm using a mysql database. It contains about 10 million records.
What it needs to do is to find records in a database with zipcode and house number as parameters. When it does not find a record, it needs to do a query with only the zipcode and get the record with the lowest house number. When the zipcode cannot be found in the database the application needs to deal with this.
Thus the code for doing single queries looks like this:
Statement select = "select * from zipcode_addresses where zipcode = ? and houseNo =?";
ResultSet rs = select.executeQuery();
if(rs.next()) {
dealWithResult(rs);
}
else {
Statement alternativeSelect = "select * from zipcode_addresses where zipcode = ? group by houseNo having min(houseNo)";
ResultSet rs = alternativeSelect.executeQuery();
if(rs.next()) {
dealWithResult(rs);
} else {
System.err.println("Could not find zipcode :" + zipcode);
}
}
Is there a proper way of doing batch select queries which deals with data not being found?
Thanks!
Update
The table structure is the following:
+-----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-------------+------+-----+---------+-------+
| zipcode | varchar(6) | NO | PRI | NULL | |
| house_no | int(11) | NO | PRI | NULL | |
| sanddcode | varchar(45) | NO | | NULL | |
| depot | varchar(3) | NO | | NULL | |
| network_point | varchar(6) | NO | | NULL | |
| region | varchar(3) | NO | | NULL | |
| seq | int(11) | NO | | NULL | |
| cluster_id | varchar(1) | NO | | NULL | |
| strand_id | int(11) | NO | | NULL | |
| strand_props_id | int(11) | NO | | NULL | |
| version_id | int(11) | NO | PRI | NULL | |
+-----------------+-------------+------+-----+---------+-------+
Primary key on version id, zipcode and house_no
Index on zipcode and house_no and another index on zipcode, both using BTREE index.
The application might sometimes be used to do 1 million distinctive select queries at which point it just takes too long.
Your code snippet doesn't show how your statements are being prepared. If your statements are being called numerous times then you should take a look at the PreparedStatement object:
http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html
Your statements can then be cached to reduce future overhead.
you could create a stored procedure with two parameters, and use the House Number as optional, or just let that the procedure finds if exists or don't.
A lot depends on the usage pattern. How many queries you run, how often there is a ZIP code miss, etc. First off, I would use PreparedStatements where posssible. I am not that familiar with MySQL, but they are usually cached and reused by the connection-database, that will help with performance. Next, If ZIP code misses were common, I would probably build an in memory cache of ZIP codes to short circuit doing 3 queries on a miss. After that, I might make a view that's ZIP + house number. Going further depends more on how your appliation works, but these things would help.
The 'group by' in your second SQL query is unnecessary and killing performance. For maximum performance, replace this select (the second one in your code) ...
select * from zipcode_addresses where zipcode = ?
group by houseNo having min(houseNo)
with this ...
select min(houseNo) from zipcode_addresses where zipcode = ?
Also, ensure you have an index on zipcode + houseNo (which it looks like you have - from the updated post).
How to get the Name of the DB field causing ConstraintViolationException while inserting in to Database in hibernate.
I have the Table Like
mysql> desc Mytable;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | bigint(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | UNI | NULL | |
| city | varchar(20) | YES | UNI | NULL | |
+-------+-------------+------+-----+---------+----------------+
And records inthe table are
mysql> select * from Mytable;
+----+--------+-------+
| id | name | city |
+----+--------+-------+
| 1 | SATISH | BLORE |
+----+--------+-------+
1 row in set (0.00 sec)
Now, im trying to insert
"RAMESH","BLORE" through hibernate.
It is throwing ConstraintViolationException due to "BLORE" (CITY) already Exist.
if im trying to insert.
"SATISH","MLORE" through hibernate
It is throwing ConstraintViolationException due to "SATISH" (NAME) already Exist.
My question is
how to get fieldName who is causing the exception ConstraintViolationException through Hibernate.
Since there might be other constraints that could be violated (e.g. combined keys), you only have the name of the constraint that is violated, which in your case might just be the column name (however, I'm not entirely sure about that). You can get the name of the violated constraint by calling getConstraintName() on the ConstraintViolationException.