I have a requirement to load property code and description (an object will hold these values) from database. Currently I have these values being populated from properties file.
The values of these properties depend on the downstream system sending the response. I even have default values if I don't get a matching property from downstream.
What I have done so far:
Connect to Oracle db using hibernate to retrieve values from db.
Join two tables (one for default and another for system specific values) to get the data. Form list of objects from received data.
Compare data received from db with String variables I already have. Add property code and description if the variable name match with property name from db.
Though the code works as expected, this is not a clean solution. Can someone please suggest better approach for the same.
Related
How do I change the method of storage on Rails when using the redis-rails gem. I set the cache store to use redis, and then used Rails.cache.fetch as documented. It works fully as expected, however when retrieving the key the output is not some easily editable format. Using redis-desktop-manager here is the output:
http://imgur.com/cE1pAy1
(no sensitive info, randomly generated). Is there any way to have it stored as JSON? My end goal is to be able to connect to the redis server using Java and updating fields for specific users in the redis array.
Here is how I used the cache:
Rails.cache.fetch("some_key", expires_in: 1.day) do
User.all.to_a.each do |user|
user.name = "foobar"
end
Later, from Java, I'd like to be able to connect to Redis and set some users name by ID or some other attribute. How can I either deserialize the Redis data OR store it in something like JSON to easily parse and change attributes?
EDIT Okay, I found a way to override the Marshalling code in redis-store (required by redis-rails) to use YAML, which I can deserialize. Now my next issue is this: Is it a bad use of redis to be storing a YAML file representing an array of users into a single key, and then to be updating the entire yaml file (for just one or two increment or decrement changes) often in Java?
The current YAML file is a list of users and their kills and deaths in a game written in Java. The Java game will need to often update a key in the YAML file, which means getting the entire YAML file, parsing it, changing a value, going back to a YAML format, and setting that as the key value. How inefficient is this exactly (should I be worrying) and how can I make the Rails redis prefer storing each user as a separate key if needed?
I just overrode the marshalling code. I reopened https://github.com/redis-store/redis-store/blob/master/lib/redis/store/marshalling.rb
And replaced the Marshall::dump with YAML::dump, along with the load
I am working on an Java application which uses MySQL database as the data storage layer. There are few configuration tables in database, but each table has many thousands of records / rows. These all configuration is cached / loaded in memory in corresponding data structures / beans(JAVA POJO's) when application starts up.
Everything is fine except that every time the application starts the caching takes place and this usually takes 15-20 minutes, as the data to be cached is huge and also some columns have XML string which is parsed and then stored in beans.
So what's the big deal??
Why should we cache when no data is changed between consecutive start-up's.?? I can have all the beans encapsulated in a common Config bean and serialize it. And load this serialized object the next time when I figure out no data is changed - and yes of course loading serialized object is far faster then database hit plus bean population.
So is there any way I can figure this out?
Of course at database level. I would query when the application starts - Was there any change in the database tables since it was last started. If yes do the same old boring caching process and store some unique identifier and serialize, Or if last identifier and current identifier are same just load the serialized object. This unique identifier will of course be persistent.
Add an last_updated column of type timestamp to the table.
When you need to check if there are changes on the table simply execute the query:
select max(last_updated) from YOUR_TABLE
If the last_updated is after the time you created the last cache copy you can update the cache with only the elements changed since last creation of the cache with a query similar to this one:
select * from YOUR_TABLE where last_updated > LAST_CACHE_UPDATE
As explained in the comments is higly recomandable to add an index on the column last_updated. Using an index give you the possibility to retrieve the maximum value in a table of 1.000.000.000 records in 30 steps (not 1.000.000.000 as wrong mentioned in the comments).
If you restart your application a lot and your cache can live in off memory data structure like redis or hazelcast, use that as cache, not the jvm memory. When update data, update both sides.
I am writing a web api using Spring and Postgres.
I have a case where I take a Json object item
The uri is /api/item/{itemId}
Request type is PUT
Json:
{
"name":"itemname",
"description":"item description here"
}
So I do (using JdbcTemplate) a SELECT statement to check if the itemId exists and then update if it does.
I would also like to implement a case with partial puts taking Json that look like this::
{
"name":"itemname"
}
OR
{
"description":"item description here"
}
where only the respective fields are updated. In Spring, the variables not present are automatically null.
The way this is implemented now is:
SELECT all columns from the items table
Sequentially check every single expected variable for null and if they are null, replace the null with the value selected from the table in step 1.
UPDATE all columns with the values (none of which should be null if the table has a not null constraint)
Question: How do you do this without == null or != null checks? Is seems to be poor design and involves iterating through every single expected variable for every single PUT request (I will have many of those).
Desired responses (in order of desirability):
There's a way in Postgres where if a null value is input, the column-value is simply not written to the database (and no error is produced)
There is a way to use Spring (and Jackson) to create a Java object with only the provided values and a way to generate SQL and JdbcTemplate code that only updates those specific columns.
Patch is the way of life - implement it
Change the front-end to always send everything
You have two choices when working with the database:
Just update what has changed, doing everything by yourself.
Get Jackson and Hibernate to do it for you.
So let's look at No. 1:
Let's say you're looking right now at the contents of an html form that has been sent back to the server. Take every field in the html form and update only those fields in the database using an SQL statement. Anything that is not in the form will not get updated in your database table. Simple! Works well. You don't need to worry about anything that is not in the form. You can restrict your update to the form's contents.
In general this is a simple option, apart from one problem, which is html checkboxes. If you are doing an update, html checkboxes can catch you out because due to a little design quirk, they don't get sent back to the server if they are unchecked.
No. 2: Perhaps you're looking for Hibernate, which you didn't mention. Jackson will fill a json object for you (must have a record id). Use Hibernate to populate a java class with the existing record, update with the new values Jackson has provided, then you tell Hibernate to merge() it into the existing record in the database, which it will. I also use Roo to create my Hibernate-ready classes for me.
No. 2 is hard to learn and set up, but once you've done sussed it, it's very easy to change things, add fields and so on.
I have followed Balusc's 1st method to create dynamic form from fields defined in database.
I can get field names and values of posted fields.
But I am confused about how to save values into database.
Should I precreate a table to hold values after creating form and
save values there manually (by forming SQL query manually)?
Should I convert name/value pairs to JSON objects
and save?
Should I create a simple table with id,name,value field and
save name/value pairs here (Like EAV Scheme)?
Or is there any way for persisting posted values into database?
Regards
It look like that you're trying to work bottom-up instead of top-down.
The dynamic form in the linked answer is intented to be reused among all existing tables without the need to manually create separate JSF CRUD forms on "hardcoded" Facelets files for every single table. You should already have a generic model available which contains information about all available columns in the particular DB table (which is Field in the linked answer). This information can be extracted dynamically and generically via JPA metadata information (how to do that in turn depends on the JPA provider used) or just via good 'ol JDBC ResultSetMetaData class once during application's startup.
If you really need to work bottom-up, then it gets trickier. Creating tables/columns during runtime is namely a very bad design (unless you intend to develop some kind of DB management tool like PhpMyAdmin or so, of course). Without the need to create tables/columns runtime, you should basically have 3 tables:
1 table which contains information about which "virtual" DB tables are all available.
1 table which contains information which columns one such "virtual" DB table has.
1 table which contains information which values one such column has.
Then you should link them together by FK relationships.
My app uses a SQLite database for the information. I have a function that checks to see if the folder and database are already present, if they aren't it will go on the internet ( currently I am using dropbox to store the db file ) and download the database and store it on the sd card, then I it will open the database. The database is writable as it lets the user rate an object. I have two questions.
1.) I would love to provide updates to the database and then have my app update the database if the version number is higher and replace the existing one. I have done some research and from what I have found it is possible to store an xml or json file with the version number of and the just parse the information and if the version number is higher download the new database.
Can someone provide an example of how this is accomplished and whether it is better to use xml or json for this task?
2.) Is there a way to save the rating in the new version of the database when the new is downloaded and accessed?
Thanks
two nights ago I wrote something like that.
pack your database structure as an array in a webservice method by reading field names and field types. the structure of array is arbitrary.
call web service method and you must receive a string that represent a JSONArray object, if you sent it as json with json_encode() method in php.
read structure and make CREATE DB query string with for loops.
execute query, so you must have database.
also you can send alot of information with arrays.
introducing each part is hard, so for each part google it.
don't forget to convert field types to match SQLite types such as VARCHAR=>TEXT, smallint=>INTEGER , ...