Spring, Hibernate and Ehcache - Wrong entities - java

I've got a webapp which uses spring+hibernate for my data layer. I'm using 2nd level caching with ehcache as provider. Everything seems to work so far but sometimes we encounter a problem which I can't really figure out atm.
One of my tables is used for labels within the application - every user who logs access this table with his set language. Works for 90% of the time. But sometimes the user gets labels for the wrong language, e.g. instead of german everything turns to italian.
After a logout and login all labels are correct.
Does anyone of you encountered something like this? I'm not sure where to look at: spring+hibernate+ehcache is a solid package or is it not?
Cheers

Spring/Hibernate/EhCache is a solid stack, I'm 99% sure it's a bug in your code.
Any chance the labels are cached statically somewhere in your application?
Make sure equals and hashcode are proprerly implemented in your entity classes, you can find insights on how to do it here
Try to remote debug your server, set breakpoints in your DAOs to check that the data is actually fetched.

Related

Hibernate find by ID slower than find all

So, for starters, i'm working on an application with an angular front end, Java 8 middleware, and Oracle 12c back end. UI sends the request to the java middleware via REST, middleware access oracle via Hibernate.
In my application, I have an entity type A that has many relationships to other entities. I'm experiencing an issue that when do an entity manager .find for an entity A via its ID, queries for some of these entities are taking upwards of 15 seconds. It seems specific to when type A entity has relationships to a specific type B entity, and I've tried lazily and eagerly fetching to no avail. The problem persists regardless of how it's fetched, and I've logged the time down through all layers of the application, and the slow down is specifically when the find by ID is done at the facade level. That said, if I do a find all query for all type A entities, then the query completes in a reasonable amount of time for the number of entities currently in the database (i.e. much less than 15 seconds). I've tried searching for similar cases and reading documentation, but I can't find a valid reason why this would be happening. Hoping someone with more hibernate experience than myself has run into a similar issue, and can point me in the right direction. Will be happy to elaborate further if needed, but not completely sure what is going on under the hood of hibernate that would explain this behavior. Thank you.
[edit] Thanks for the replies, and apologies for the vagueness. There are a lot of things that affected my ability to be more thorough, or get the exact SQL statements, etc. For anyone who may find this in the distant future, the issue was related to a custom annotation for 2 entity C objects in entity A. I have not gotten exactly to the bottom of the issue as of yet, but have confirmed that was the problem and wanted to update here to say that it's not going to be a general Hibernate issue, but rather something more specific to a custom annotation.

Hibernate: force 'with (NOLOCK)' hint on all select queries, without changing isolation level

Quick background story:
I work on a very old application that has recently been having issues with locks on the database. The app is written in Java and uses Hibernate. One of the issues we identified are transactions that are kept alive unnaturally long while also having isolation levels changed between READ_COMMITED and READ_UNCOMMITED frequently. While we acknowledge that the clear solution is refactoring the code so that transactions are smaller, this would be an enormous effort that we cannot afford entirely right now (most used parts of the app are being migrated to a new system but this procedure is relatively slow).
So - because we use READ_UNCOMMITED for all our Select operations and READ_COMMITED for everything else, a DBA that has been helping us, identified a possible solution in changing the isolation level to a global READ_COMMITED and changing all select queries to include the hint 'with (NOLOCK)'. He says functionally there should be no difference in the way data is retrieved (since we use dirty reads right now with no problem) while providing us with an advantage in not having to frequently change isolation level within the transaction. I believe his idea also comes in regards to recent reports we've been having about database locks being caused by isolation level changes.
So - Can we (and if so, how?) tell hibernate to add a 'with (nolock)' hint on all queries being automatically generated by the usage of mapped java objects and HQL (and maybe even existing SQL being passed to hibernate, though this seems like pushing it :) ) WITHOUT changing the isolation level?
Final side notes: we are using an older version of hibernate, v3.5 and right now an upgrade is unlikely, some incredibly 'smart' people decided to taint it at some point, inserting some of their own code that the application uses. Upgrading has been tried and failed multiple times.
Also: i have checked quite a few related threads, the general idea seems to be: don't use nolock, change isolation level, which - as stated - we're not looking to do.
Edit1: Since the app has been continuously developed in the past 12 years, there are loads of modules that haven't been even once glanced over by the current dev team, the ideal solution would be something that doesn't require the identification of every single bit of Java code that uses persisted objects.
Edit2: A possible way to go about this - should Hibernate allow it - would be to add a form of Interceptor that receives the formatted SQL query before being passed to the db driver. I would then take care of adding the hints myself, using some form of regex.
Thank you very much in advance.
You cannot use (NOLOCK) with HQL. You can however with native SQL if you decide to change your queries. Something like:
getCurrentSession().createSQLQuery("select * from table with(NOLOCK)").list();

Hibernate hsql queries have no effect when called from servlet

I am having problems using my DAO (Data Access Object) classes on my servlets.
Everything works fine in a simple main method when not deployed on the server, but using the same classes in my servlets doGet methods seems to have no effect on database. I still get the query messages on console, for example:
Hibernate: select something_id.something from something
but nothing returns...
Im using a memory database (jdbc:hsqldb:mem:db1) and EntityManager to insert and retrieve from it (mostly createQuery()) in my DAO classes methods.
Im testing on eclipse localhost server preview. Tried Tomcat and Glassfish also, but they didnt seem to change anything so I returned to the default.
Does anyone know what am i doing wrong or what might be the issue?
I cant provide any specific code right now, because it is in my work computer, but i will in approximately 12 hours if its needed to help solve this issue.
EDIT
Solved my problem. I foolishly executed my database schema creation file with every get request and thats why the queries didnt seem to work :). I'll be more thorough next time!

What is the best Java GUI methodology for ease of maintenance and enhancement?

I need advice on how to rewrite a java GUI. Ultimate goal is easier to maintain & enhance.
What I have built is a Java Applet Client interface that acts and behave similar to Eclipse. developer can design their data entry forms without using a single line of code (drag and drop), and define its attribute. This part is pretty well iron out. however, i am left with more than 40,000 lines of codes that is very difficult to maintain.
Each time a bug is occur or a new enhancement, i normally cant program in a more direct way. more than half the time, i need to workaround the problem and that adds up the lines of code.
Consideration:
-Java Web Applet (because it runs on any browser with J2RE installed)
-runs on slow machine
-deployment of around 200 nodes and growing
Problems that i currently have:
-Listeners are all over the place. sometimes is inside the element.AddListener(new listener..). Sometimes is outside of the class, could be in another package that contain all the rest of listener.
Question: is it always good idea to put all listener in another package? if that is that case, i cant use "this." to get the reference i need.
-JTable this is a killer to me :( the problem i had on Cannot access the Jtable column data after set invisible still persist. Imagine i have JTable with 3 column. First column is a dropDown, second and third column is a textfield. Whenever a value choose from dropdown, i need to base on the selected value, and update to the second column and third column. the problem is, if the user click and it click on other row very fast, it will update to a column that is in the wrong row.
-Currently the program is coded in the sense of it is single thread. whenever the user does a http connection to the server side, reading a file, writing a file and etc, i need to make it as asynchronous process so it doenst feel like "program hang". what is the best way to do this?
Really appreciate help here! Thanks!
Lots of questions here and I'm not sure where to start but I can sympathize with you one this one. Unless you have a well seasoned team that has already gone through the pains of Swing application development things can quickly become out of control and unmanageable.
Before you adventure into re-writing a project I would start with defining some simple standards for development. Like package structures and listeners. I would also recommend splitting the application up into well unit tested modules or sub projects.
Also, ask yourself if you really need to re-write the application or does it just need some TLC. As a consultant and Director of IT I see developers always wanting to re-write applications just because they've learned something new or don't think it's up to par. When they come to me and tell me that it's junk and needs to be re-written I usually send them back and ask them to come up with alternative solutions to a re-write and the impact of each solution including - doing nothing. In a lot of cases we didn't write the application at all.
[UPDATE]
Lastly, If you are going to re-write I would use a Domain Driven Design and MVC approach. Yes, I said MVC for desktop applications!. We've had great success with these methodologies. It keeps a good separation of concern and makes things easily re-usable. It also provides the structure to easily switch out the presentation layer. Most importantly it's easy to unit test and any developer that understands MVC can understand the basics of your project without knowing the details.
I have some more thoughts but i'll leave it at that for now. ;)
use dsl for gui:
swinghtmltemplate
swixml
yaml
there are some more of them
this will remove the need to describe listeners, allow binding in dsl manner
Why dont you just reuse the eclipse framework to build your own gui instead of writing it from scratch in Swing ?

SQL server stub for java

I have a java application that is using MSSQL server through the JDBC driver. Is there some kind of stub that I can use for testing? For example I want to test how my application handle cases of connection errors, SQL server out of disk, and other exceptions. It's pretty hard and complex to simulate this with real SQL server.
Thanks
You could write unit tests against your DAOs or repositories returning mock Connection objects using a mock library such as https://mocquer.dev.java.net/.
You'd need a really clean and decoupled application architecture though in order to make this work correctly and provide you with actual test coverage.
You could (assuming the system is architected in a way to make this easy) create your own versions of the DB Access classes (I assume you are using teh statement/preparedstatement interfaces), which would hold the real DB calls and that you can modify to do exactly what you want.
I've done this - it takes a day or so of really boring work.
I don't think there's something like that.
You'd be better off setting up your own database and testing on your machine/lan.
All I know there is out there, is:
freeSQL
db4free
Both support MySQL, but none MS-SQL. I do think that has to do with licensing issues and limitations. So I'm afraid you won't find a similar service for MS-SQL db.
Answering myself with an option I thought of, I'll be glad to hear your inputs on it.
After crawling around, I got to HyperSQLDB, a java-implemented database.
How feasible do you think is to take the source code of HSQLDB, and adding another layer to it, so I can control it and inject pre-defined behaviors to it.
For example, I'll make it run all queries slowly, I'll make it disconnect, etc.
Do you think this idea is worth pursuing? Is it doable in a reasonable amount of time?
If you use something other than MS-SQL, you may cause more testing problems due to incompatibilities and lack of functionality (e.g., transactions) than you solve. So I'm with Carl - use a shim.
If you were looking for unit-test coverage of ordinary behavior, I might think differently.
I haven't used them personally, but the stuff you're talking about sounds like a really good fit for a mocking framework, such as Mockito(docs) or PowerMock. They appear to provide good support for the kind of failure injection you're after. Can someone with experience with either of them (or similar) weigh in? See also How to stub/mock JDBC ResultSet to work both with Java 5 and 6?
execute procedure sp_who2 it will generate the all the current connections and process in your db you can see a column named spid corresponding to each db connection. just type: kill <<spid>> and execute it to terminate any users..etc. but if the spid is less than 50 it means it is a system process and dont kill it. This can help you replicate connection drops.
you can also say ALTER DATABASE dbname SET SINGLE_USER WITH ROLLBACK_IMMEDIATE this will drop all connections to the said db immediately.
Select ##MAX_Connections as Max_Connections would give you the max connections which can be made to a database (you can set it to a low number to test connection unavailability).
to replicate query timeout.. set the query timeout to a very low number & execute a fairly large query.
to create disk space error, simply redice the size of the db file & do not allow it to grow... then insert data to the database (you'll get an exception).
altert database xxx (file= maxsize= filegrowth=)

Categories