I have some ids (id from database, example 34645) that I currently log as "[34645] - something happended" using something like:
log.info("[" + id + "]" + foo);
Some logs, like "server starting", "database connection bla" dont have an id and thus doesn't log any and that's fine.
However, when I have an id I call methods that also log, but don't have the id, like:
lookup(name) {
//do some lookup and stuff
log.info("[" + name + "]" has some info we use somewhere: " + result);
}
Is there a (smart) way to get the id logged inside lookup() without passing id to lookup() or refactor class hierarchies? There are different threads logging so setting/unsetting id-values for logback to use will probably be difficult to get right.
as per request and I like credits, you can use MDC for that thing.
Info on that is here: http://logback.qos.ch/manual/mdc.html
Thanks :)
Related
I have a simple spring web application, which is connected to a Postgre db. My question is I have method in dao, which is annotated with #Cacheable. Is there a way to log if the method goes to db, or its result is loaded from cache? For example, I'd like to see the following log:
The value is retrieved from db....
The value is retrieved from cache
You can enable trace logs for CacheAspectSupport. This is probably going to give you too much information though.
In case of a cache hit you'll get
Cache entry for key '" + key + "' found in cache '" + cache.getName() + "'"
And a cache miss
"No cache entry for key '" + key + "' in cache(s) " + context.getCacheNames()
There is no hookpoint to configure caching so that it calls you when those things happen. You may want to look at your cache library to see if they offer some hook point.
I'm using Morphia 1.5.2 (Java 8) as a driver for MongoDB (V4.x), trying to use Search for a phrase, so my code looks like :
datastore.find(myEntity).disableValidation().search("\\\"" + textToFilter + "\\\"");
Debug looks good, but in running time the query is being sent with the three backslashes instead of just one, and the query return 0 results.
What am I missing? thanks!
actual generated query: "$text" : { "$search" : "\\\"filteredText\\\"" }
try this:
datastore.find(myEntity).disableValidation().search("\"" + textToFilter + "\"");
Copied and pasted from the official github issue tracker at https://github.com/MorphiaOrg/morphia/issues/1453 . I would have proposed this as an edit to a previous answer, as a reasonable person would have, but the moderators decided to delete the answer instead. Hope you weren't too delayed in getting your answer.
datastore.find(myEntity).disableValidation().search("\"" + textToFilter + "\"");
Thanks #evanchooly !
In my (java) Controller in a Play2 project I'm saving some data to an object.
So entity here is an instance of a Model subclass.
I do stuff like this
log.debug("Saving title=" + title + ", tags=" + tags);
entity.title = title;
entity.tags = tags;
entity.save();
// verify:
ModelClass m = ModelClass.find.byId(entity.id);
log.debug("Saved title=" + m.title + ", tags=" + m.tags);
Where title is a String and tags is a List<String>. The debug log says
Saving title=foo, tags=[bar, quux]
Saved title=foo, tags=null
So data is coming in, I'm not getting any warnings, but the list of strings is just lost somewhere along the way. I'm just using an in-memory h2 db, maybe it works when I'm really persisting it, but... what's up with this?
Edit: The generated SQL create syntax doesn't contain "tags" at all. So there's obviously something wrong with that.
Edit: see How to persist a property of type List<String> in JPA?
In JPA you must declare a List as #ElementCollection for it to be persisted. It seems that EBean do not support this feature.
One way to do it should be to declare your List tags as #Transient (ie. not persisted) and have methods to manage it while keeping up to date a simple String that contains your tags comma separated. That would be this String that gets persisted in a single column.
I got a little question about databases and android. I got this code:
sampleDB = this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVATE, null);
sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " +
SAMPLE_TABLE_NAME +
" (LastName VARCHAR, FirstName VARCHAR," +
" Country VARCHAR, Age INT(3));");
sampleDB.execSQL("INSERT INTO " +
SAMPLE_TABLE_NAME +
" Values ('Makam','Sai Geetha','India',25);");
and to read:
if (c != null ) {
if (c.moveToFirst()) {
do {
String firstName = c.getString(c.getColumnIndex("FirstName"));
int age = c.getInt(c.getColumnIndex("Age"));
results.add("" + firstName + ",Age: " + age);
}while (c.moveToNext());
}
}
With this code, I make and read the database, and insert some info in it. And print it on the screen, this all works :)
Now the part I can't figure out:
I use myPHPadmin (with xampp),
I made the exact database as I do in the code.
But how do I connect, so my code reads that database.
It is a local database for now (127.0.0.1).
Is it possible to connect a local database? (if so, could you tell me how to)
Do you need PHP, or can you do everything in (Android) Java code?
I am totally new with databases, so sometimes it confusing for me.
Please put me in the good direction.
If you need more information for the question or something else, please let me know.
It is a local database for now (127.0.0.1).
In Android you have to use 10.0.2.2 or System's Static IP.
Write a PHP script (You can also use other but PHP its easy to implement) to manage the database and run this script using HTTP protocol from the android system.
These Tutorials might help you:
Step-by-Step-Method-to-Access-Webservice-from-Andr
Web Services - An XML-RPC Client for Android
As far as I'm aware there is no MySQL library for android. But you can use the HttpPost to send data to a server side script (such as PHP) and then return it in a format you can parse in your Android application.
There's a nice tutorial on how to achieve this here: http://www.helloandroid.com/tutorials/connecting-mysql-database
Here's a link to the HttpPost Documentation: http://developer.android.com/reference/org/apache/http/client/methods/HttpPost.html
Hope this helps, this is a good way to get you started communicating with external MySQL databases within an Android application.
I have db column whose datatype is Number (15) and i have the corresponding field in java classes as long. The question is how would i map it using java.sql.Types.
would Types.BIGINT work?
Or shall i use something else?
P.S:
I can't afford to change the datatype within java class and within DB.
From this link it says that java.sql.Types.BIGINT should be used for long in Java to Number in SQL (Oracle).
Attaching screenshot of the table in case the link ever dies.
A good place to find reliable size mappings between Java and Oracle Types is in the Hibernate ORM tool. Documented in the code here, Hibernate uses an Oracle NUMBER(19,0) to represent a java.sql.Types.BIGINT which should map to a long primitave
I always use wrapper type, because wrapper types can be express null values.
In this case I will use Long wrapper type.
I had a similar problem where I couldn't modify the Java Type or the Database Type. In my situation I needed to execute a native SQL query (to be able to utilize Oracle's Recursive query abilities) and map the result set to a non-managed entity (essentially a simple pojo class).
I found a combination of addScalar and setResultTransformer worked wonders.
hibernateSes.createSQLQuery("SELECT \n"
+ " c.notify_state_id as \"notifyStateId\", \n"
+ " c.parent_id as \"parentId\",\n"
+ " c.source_table as \"sourceTbl\", \n"
+ " c.source_id as \"sourceId\", \n"
+ " c.msg_type as \"msgType\", \n"
+ " c.last_updt_dtm as \"lastUpdatedDateAndTime\"\n"
+ " FROM my_state c\n"
+ "LEFT JOIN my_state p ON p.notify_state_id = c.parent_id\n"
+ "START WITH c.notify_state_id = :stateId\n"
+ "CONNECT BY PRIOR c.notify_state_id = c.parent_id")
.addScalar("notifyStateId", Hibernate.LONG)
.addScalar("parentId", Hibernate.LONG)
.addScalar("sourceTbl",Hibernate.STRING)
.addScalar("sourceId",Hibernate.STRING)
.addScalar("msgType",Hibernate.STRING)
.addScalar("lastUpdatedDateAndTime", Hibernate.DATE)
.setParameter("stateId", notifyStateId)
.setResultTransformer(Transformers.aliasToBean(MyState.class))
.list();
Where notifyStateId, parentId, sourceTbl, sourceId, msgType, and lastUpdatedDateAndTime are all properties of MyState.
Without the addScalar's, I would get a java.lang.IllegalArgumentException: argument type mismatch because Hibernate was turning Oracle's Number type into a BigDecimal but notifyStateId and parentId are Long types on MyState.