Avoiding DB hits - java

In my java application, I have a dropdown box which needs to be populated with values from a table games.
There is a separate functionality to add a game. (i:e. an insert in the table games)
The values of the dropdown wont be changing unless someone adds something in the database.
To develop the functionality to populate the dropdown box,
One way is to -
Hit the table everytime a page load happens and fetch the data to populate the dropdown.
Is this an effective way? Can someone suggest me a better alternative design?

You could use a cache... everytime a game is added you simply update your cache.
Implementing a cache is simple.

If inserting data and retrieving it for the dropdown are performed in the same java application, then we might implement a kind of cache for the list of values.
So, we need a structure to store last list of games fetched from database, for example List<Game>, and a flag that is essentially an indicator for the event "someone added row to database".
Each time the page with dropdown is requested, we first check the indicator - if it is true, then we need to reload the list from database and set it back to false, if it is false - we can return the cached list.
Each time user adds entry to database, we set this indicator to true.
Also please be advised of possible concurrency problems when implementing that kind of cache.

Related

Realm: Order of records was changed

I'm trying to develop my Android app with Realm database.
Today I got below problem:
I added a list of records to table and then try to deleted one of them.
after deleting the order of the rest was changed (it's different with the order before deleting).
please see the images below to see detail.
Before deleting
After delete the 3rd item
And the question is: That's is an function or an bug? And how Can I keep the order of record?
I know that I can easy to get the correct order as I want with add a new field as createTime or something like that but I want to find an very simple solution as config something for Realm.
Items in a Realm are not sorted by default, so you should think of any query result as an unordered set unless you explicitly sorted it.
Generally the items will come out in the order you inserted them in, but it is not a guarantee. The underlying reason technical reason is that we compact the data on the disk, so if you delete items in the middle of a list, the last item will be moved to its place.
So the answer is: It is working as intended, and you should use a sorting method if you want your results to be sorted.

Java Design Pattern on saving form data

I have a jsp page having many sections/categories to fill hardware configuration details and each section/category has many details to be filled in either by selecting a value in a list box or entering data in a text box. The user may fill in some fields of some section and can choose to fill other sections later. When user logs in next time to fill data, he must be shown the previously filled in data for respective sections/categories. The current design is, when user is entering any data and goes to next field, an ajax call is made to persist the entered data in DB. So if there are 10 fields in a section and if there are 10 sections in the form, 100 JDBC calls are made and if user wants to edit already entered field, additional JDBC calls are being made. Also the 10 fields in a section are dependent on each other, for example if the first field is “Operating System Name” and if I select as “Windows” then the next field “OS Version” should only show values “2000,2007 2008 etc” and the next field “OS Architecture” should only show relevant values for Windows and its Version. This was the main reason why a JDBC call is made each time when user enters a value in a field
Need your advice on this design to make minimal JDBC calls and the current design more efficient. Thanks
You could take a look at the J2EE ContextObject pattern. It'll allow you to encapsulate the state of user's configuration, and to share it throughout your application. Also the ValueListHandler will help you handle those expensive objects via caching the results, and allow the client to traverse and select items from the results.

Determine set of displayed items in JavaFX TableView

My question in its simplest form: Is it possible to determine the subset of items (or the indices of the corresponding items) currently being displayed in a JavaFX TableView?
The reason I'm asking is that I want to implement lazy loading. Therefore, I subclass ObservableList and implement the code that fetches new items from the database in the overriden E get(int index) (using prefetch/cache). However, the entity objects might be changed by the user and are therefore observed by the database backend. That's why I'd like to detach them as soon as they are not displayed any more.
Many thanks in advance for any suggestions.
You can use VirtualFlow to get the indizes of the first and the last visible cell: Scroll TableView via a Button
However, as I understood it, this is more a hack then a real part of the API. Also you might run into trouble if you have more tables around, it will be triggered each time a table changes.

How to cache complex calulations in a wicket web application

I am building a wicket web application. It shows a list of 'entities' with a DataView and also displaying a details page which works fine. Only the needed rows from the database are loaded into my DataView, so performance is good.
I can also show a calculated value on the details page, which is generated on the fly from the 'entity'. But when i want to sum up this calculated value on my list page, performance gets poor when there are several hundreds of rows in the table. This is because the value is generated for the whole table for every session.
Should i cache this calculated value (if yes, how can i do this) or would it be better to add a column in my database table that holds this value, which would violate the DRY principle?
As #biziclop commented, a solution for a simple cache is adding a field to the Application Class, which caches the calculated value. This way, the calculations must be made on application startup and on every change of relevant data.

Possible Data caching issue with flex datagrid

I have an application in which there are 5 tabs. Each tab has a datagrid. The dataprovider to the datagrid in 1st tab is the common dataprovider to datagrids in other tabs. data in each of the tab varies based on status except for the 1st tab where i display all the data. Now the data for main tab is refreshed every minute using a timer to fetch new data from the backend[Use Blazeds to interact with server side code i.e Java].
After i perform any operation on any of the row in any of the tab other than the 1st one i need to delete the row from that datagrid. I am able to do that by removing the entry from the dataprovider but as soon as i go back to the main tab and return to the tab where i had done the operation i find the deleted entry back. The point to note here is that the entry is not there in the main tab nor it is in the tab where i performed the operation but as soon as i navigate between the tabs it is shown in the UI. But if i do a refresh using F5 it behaves normally.
I was just wondering whether this issue is due to the data caching on the client side and if so what can be a solution to this ?
After you delete the object from the arrayCollection / data - run a collectionName.refresh() on it.
That said, my best guess is that I think you're running into a situation where you're updating a pointer or copy ref rather than the actual data and it's getting reset into that pointer with binding or similar.
If you add a binding listener / change watcher on the object that is bound to the datagrid that traces to console when it gets a CHANGE event, I bet you get a surprise!
(btw, this is how to debug this type of issue quickly).
From your situation its very hard to say without looking at your code. I do agree with the comments passed by the Flextras.
once you deleted it does not show in the tab and when you navigate
to Main tab it does not either show
there and only when you come back
again to the the same tab it does not
get reflected.
It seems there could be an issue with your DB Queries [May be]. Why don't you recheck with your queries where once the delete operation is performed, does the DB tables get reflected or not.
If the above turns out to be false, then get back to the next step.
What does trace() of the dataprovider
give you. I mean what does it
contain? Try this on the select event
of the tab.
Your dataprovider May be holding previous values, though the item from
the grid being deleted is not reflecting the dataprovider. Did you
try to refresh the datagrid or why don't you call invalidateProperties()
method to check.
Once again a some piece of code tabs
could help you and us a lot of time in giving a correct riposte to the question.

Categories

Resources