Is System.nanoTime() completely useless?

Question: As documented in the blog post Beware of System.nanoTime() in Java, on x86 systems, Java's System.nanoTime() returns the time value using a CPU specific counter. Now consider the following case I use to measure time of a call:
long time1= System.nanoTime();
foo();
long time2 = System.nanoTime();
long timeSpent = time2-time1;

Now in a multi-core system, it could be that after measuring time1, the thread is scheduled to a different processor whose counter is less than that of the previous CPU. Thus we could get a value in time2 which is less than time1. Thus we would get a negative value in timeSpent.
Considering this case, isn't it that System.nanotime is pretty much useless for now?
I know that changing the system time doesn't affect nanotime. That is not the problem I describe above. The problem is that each CPU will keep a different counter since it was turned on. This counter can be lower on the second CPU compared to the first CPU. Since the thread can be scheduled by the OS to the second CPU after getting time1, the value of timeSpent may be incorrect and even negative.
Comment: I don't have an answer but I do agree with you. Maybe it should be considered a bug in the JVM.
Comment: that post is incorrect and not using TSC is slow but you have to live with: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6440250  Also TSC can be made useful via hypervisor but then it's slow again.
Comment: And of course, you can run in a virtual machine where a CPU can show up mid-way through a session :D
Answer: This answer was written in 2011 from the point of view of what the Sun JDK of the time running on operating systems of the time actually did. That was a long time ago! leventov's answer offers a more up-to-date perspective.
That post is wrong, and nanoTime is safe. There's a comment on the post which links to a blog post by David Holmes, a realtime and concurrency guy at Sun. It says:

System.nanoTime() is implemented using the QueryPerformanceCounter/QueryPerformanceFrequency API [...] The default mechanism used by QPC is determined by the Hardware Abstraction layer(HAL) [...] This default changes not only across hardware but also across OS versions. For example Windows XP Service Pack 2 changed things to use the power management timer (PMTimer) rather than the processor timestamp-counter (TSC) due to problems with the TSC not being synchronized on different processors in SMP systems, and due the fact its frequency can vary (and hence its relationship to elapsed time) based on power-management settings.

So, on Windows, this was a problem up until WinXP SP2, but it isn't now.
I can't find a part II (or more) that talks about other platforms, but that article does include a remark that Linux has encountered and solved the same problem in the same way, with a link to the FAQ for clock_gettime(CLOCK_REALTIME), which says:

Is clock_gettime(CLOCK_REALTIME) consistent across all processors/cores? (Does arch matter?  e.g. ppc, arm, x86, amd64, sparc).

It should or it's considered buggy.
However, on x86/x86_64, it is possible to see unsynced or variable freq TSCs cause time inconsistencies. 2.4 kernels really had no protection against this, and early 2.6 kernels didn't do too well here either. As of 2.6.18 and up the logic for detecting this is better and we'll usually fall back to a safe clocksource.
ppc always has a synced timebase, so that shouldn't be an issue.

So, if Holmes's link can be read as implying that nanoTime calls clock_gettime(CLOCK_REALTIME), then it's safe-ish as of kernel 2.6.18 on x86, and always on PowerPC (because IBM and Motorola, unlike Intel, actually know how to design microprocessors).
There's no mention of SPARC or Solaris, sadly. And of course, we have no idea what IBM JVMs do. But Sun JVMs on modern Windows and Linux get this right.
EDIT: This answer is based on the sources it cites. But i still worry that it might actually be completely wrong. Some more up-to-date information would be really valuable. I just came across to a link to a four year newer article about Linux's clocks which could be useful.
Comment: Even WinXP SP2 appears to suffer. Running the original code sample, with `void foo() { Thread.sleep(40); }` I got a negative time (-380 ms!) using a single `Athlon 64 X2 4200+` processor
Comment: Good answer, should add a link to the more recent exploration of this topic: http://shipilev.net/blog/2014/nanotrusting-nanotime/
Comment: Note: OpenJDK didn't uphold to the spec up until OpenJDK 8u192, see https://bugs.openjdk.java.net/browse/JDK-8184271. Make sure to use at least as fresh version of OpenJDK 8, or OpenJDK 11+.
Comment: @SOFe: Oh, that's a shame. It is in the [web archive](http://web.archive.org/web/20130919092818/http://geekwhisperer.blogspot.co.uk/2010/01/twisty-maze-of-linux-clocks-all.html), fortunately. I will see if i can track down a current version.
Comment: I don't suppose there are updated on this, wrt. behavior on Linux, BSD or other platforms?
Comment: Note: the new link is broken. Can anyone post an abstract here?
Comment: @leventov I've rolled back your edit, as it's adding significant new content rather than improving what is already there. I encourage you to post a new answer with your observations - i will happily link to and upvote it!
Answer: Since Java 7, System.nanoTime() is guaranteed to be safe by JDK specification. System.nanoTime()'s Javadoc makes it clear that all observed invocations within a JVM (that is, across all threads) are monotonic:

The value returned represents nanoseconds since some fixed but arbitrary origin time (perhaps in the future, so values may be negative). The same origin is used by all invocations of this method in an instance of a Java virtual machine; other virtual machine instances are likely to use a different origin.

JVM/JDK implementation is responsible for ironing out the inconsistencies that could be observed when underlying OS utilities are called (e. g. those mentioned in Tom Anderson's answer).
The majority of other old answers to this question (written in 2009–2012) express FUD that was probably relevant for Java 5 or Java 6 but is no longer relevant for modern versions of Java.
It's worth mentioning, however, that despite JDK guarantees nanoTime()'s safety, there have been several bugs in OpenJDK making it to not uphold this guarantee on certain platforms or under certain circumstances (e. g. JDK-8040140, JDK-8184271). There are no open (known) bugs in OpenJDK wrt nanoTime() at the moment, but a discovery of a new such bug or a regression in a newer release of OpenJDK shouldn't shock anybody.
With that in mind, code that uses nanoTime() for timed blocking, interval waiting, timeouts, etc. should preferably treat negative time differences (timeouts) as zeros rather than throw exceptions. This practice is also preferable because it is consistent with the behaviour of all timed wait methods in all classes in java.util.concurrent.*, for example Semaphore.tryAcquire(), Lock.tryLock(), BlockingQueue.poll(), etc.
Nonetheless, nanoTime() should still be preferred for implementing timed blocking, interval waiting, timeouts, etc. to currentTimeMillis() because the latter is a subject to the "time going backward" phenomenon (e. g. due to server time correction), i. e. currentTimeMillis() is not suitable for measuring time intervals at all. See this answer for more information.
Instead of using nanoTime() for code execution time measurements directly, specialized benchmarking frameworks and profilers should preferably be used, for example JMH and async-profiler in wall-clock profiling mode.
Answer: I did a bit of searching and found that if one is being pedantic then yes it might be considered useless...in particular situations...it depends on how time sensitive your requirements are...
Check out this quote from the Java Sun site:

The real-time clock and
  System.nanoTime() are both based on
  the same system call and thus the same
  clock.
With Java RTS, all time-based APIs
  (for example, Timers, Periodic
  Threads, Deadline Monitoring, and so
  forth) are based on the
  high-resolution timer. And, together
  with real-time priorities, they can
  ensure that the appropriate code will
  be executed at the right time for
  real-time constraints. In contrast,
  ordinary Java SE APIs offer just a few
  methods capable of handling
  high-resolution times, with no
  guarantee of execution at a given
  time. Using System.nanoTime() between
  various points in the code to perform
  elapsed time measurements should
  always be accurate.

Java also has a caveat for the nanoTime() method:

This method can only be used to
  measure elapsed time and is not
  related to any other notion of system
  or wall-clock time. The value returned
  represents nanoseconds since some
  fixed but arbitrary time (perhaps in
  the future, so values may be
  negative). This method provides
  nanosecond precision, but not
  necessarily nanosecond accuracy. No
  guarantees are made about how
  frequently values change. Differences
  in successive calls that span greater
  than approximately 292.3 years (263
  nanoseconds) will not accurately
  compute elapsed time due to numerical
  overflow.

It would seem that the only conclusion that can be drawn is that nanoTime() cannot be relied upon as an accurate value. As such, if you do not need to measure times that are mere nano seconds apart then this method is good enough even if the resulting returned value is negative. However, if you're needing higher precision, they appear to recommend that you use JAVA RTS.
So to answer your question...no nanoTime() is not useless....its just not the most prudent method to use in every situation.
Comment: @pdeva - but you are misunderstanding what the doc says. **You are raising a non-issue.** There is some point in time that is considered "0". The values returned by nanoTime() are accurate relative to that time. Its a monotonically increasing timeline. You just might be getting a series of numbers from the negative portion of that timeline. `-100`, `-99`, `-98` (obviously much bigger values in practice). They are going in the correct direction (increasing), so there is no issue here.
Comment: > this method is good enough even if the resulting returned value is negative.
I dont get this, if the value in timespent is negative, then how is it useful at all in measuring the time taken in foo()?
Comment: its ok because all you are worried about is the absolute value of the difference. ie if your measurement is time t where t = t2 - t1 then you want to know |t|....so what if the value is negative...even with the multi core problem the impact is rarely going to be a few nanoseconds anyway.
Comment: To backup @Aaron: both t2 and t1 might be negative but (t2-t1) must not be negative.
Comment: aaron: that is exactly what my point is. t2-t1 should never be negative otherwise we have a bug.
Comment: t2 >= t1 is also true if t0 (the start time) is < 0.
Comment: Also, according to the Java docs, you can still get a negative value even on a single core machine since you have no guarantee of when your code will be executed..."the value represents nanoseconds since some fixed but arbitrary time (perhaps in the future, so values may be negative)"...
Comment: mezoid: If that was true, i.e. if the "fixed time" could change at any moment, then nanoTime would be completely useless because you have no way to tell. If the fix point is set during startup, it must not change and t2 must always be >= t1. Which means there is a bug.
Comment: What is missed here is that due to wall clock drift, service adjustments and generally poor resolution, System.currentTimeMills() comparisons (to calculate duration) can commonly result in "0" even though tens or hundreds of milliseconds have *actually elapsed*. So it is an issue of accuracy when measuring elapsed time of any operation that runs in under a second - such as when logging duration or cumulation calculation of very fast event durations.
Answer: No need to debate, just use the source.
Here, SE 6 for Linux, make your own conclusions:
jlong os::javaTimeMillis() {
  timeval time;
  int status = gettimeofday(&time, NULL);
  assert(status != -1, "linux error");
  return jlong(time.tv_sec) * 1000  +  jlong(time.tv_usec / 1000);
}

jlong os::javaTimeNanos() {
  if (Linux::supports_monotonic_clock()) {
    struct timespec tp;
    int status = Linux::clock_gettime(CLOCK_MONOTONIC, &tp);
    assert(status == 0, "gettime error");
    jlong result = jlong(tp.tv_sec) * (1000 * 1000 * 1000) + jlong(tp.tv_nsec);
    return result;
  } else {
    timeval time;
    int status = gettimeofday(&time, NULL);
    assert(status != -1, "linux error");
    jlong usecs = jlong(time.tv_sec) * (1000 * 1000) + jlong(time.tv_usec);
    return 1000 * usecs;
  }
}

Comment: That's helpful only if you know what the used API does. The used API is implemented by the operating system; this code is correct wrt. the specification of the used API (clock_gettime/gettimeofday), but as others pointed out, some non-up-to-date operating systems have buggy implementations.
Answer: Linux corrects for discrepancies between CPUs, but Windows does not. I suggest you assume System.nanoTime() is only accurate to around 1 micro-second. A simple way to get a longer timing is to call foo() 1000 or more times and divide the time by 1000.
Comment: Could you provide a reference (behaviour on Linux and Windows)?
Comment: Unfortunately the proposed method will commonly be highly imprecise because each event falling into the +/- 100ms wall clock update slot will often return zero for sub-second operations. The sum of 9 operations each with duration of zero is, well, zero, divided by nine is ... zero. Conversely, using System.nanoTime() will provide relatively accurate (non-zero) event durations, which then summed and divided by the number of events will provide a highly precise average.
Comment: Apologies as there was some confusion about the language used in "summing" events. Yes, if the time is marked at the beginning of say 1000 sub-second operations, they run and then time is marked again at the end and divided - that would work for some system under development to get an good approximation of duration for a given event.
Comment: @DarrellTeague summing 1000 events and adding them up is the same as the end to end time.
Comment: @DarrellTeague System.nanoTime() is accurate to 1 micro-second or better (not 100,000 microseconds) on most systems. Averaging many operations is only relevant when you get down to a few micro-seconds, and only on certain systems.
Comment: This is not right according to the System.nanoTime() spec. JVM takes responsibility for ironing out discrepancies on all operating systems. OpenJDK seems to succeed at that since 8u192, where https://bugs.openjdk.java.net/browse/JDK-8184271 has been fixed.
Answer: Absolutely not useless. Timing aficionados correctly point out the multi-core problem, but in real-word applications it is often radically better than currentTimeMillis().
When calculating graphics positions in frame refreshes nanoTime() leads to MUCH smoother motion in my program.
And I only test on multi-core machines.
Answer: I have seen a negative elapsed time reported from using System.nanoTime(). To be clear, the code in question is:
    long startNanos = System.nanoTime();

    Object returnValue = joinPoint.proceed();

    long elapsedNanos = System.nanoTime() - startNanos;

and variable 'elapsedNanos' had a negative value. (I'm positive that the intermediate call took less than 293 years as well, which is the overflow point for nanos stored in longs :)
This occurred using an IBM v1.5 JRE 64bit on IBM P690 (multi-core) hardware running AIX. I've only seen this error occur once, so it seems extremely rare. I do not know the cause - is it a hardware-specific issue, a JVM defect - I don't know. I also don't know the implications for the accuracy of nanoTime() in general.
To answer the original question, I don't think nanoTime is useless - it provides sub-millisecond timing, but there is an actual (not just theoretical) risk of it being inaccurate which you need to take into account.
Comment: Alas, seems to be some OS/hardware issue. The documentation states that the core values may be negative but (larger negative minus smaller negative) should still be a positive value. Indeed the assumption being that in the same thread, the nanoTime() call should always return a positive or negative value. Have never seen this on various Unix and Windows systems over many years but sounds possible especially if the hardware/OS are splitting this seemingly atomic operation across processors.
Comment: @BasilVandegriend it is not a bug anywhere. As per the documentation, rarely the second System.nanoTime() in your example can run on a different CPU and the nanoTime values calculated on that CPU could just happen to be lower than the values calculated on the first CPU. So, a -ve value for elapsedNanos is possible
Answer: No, it's not... It just depends on your CPU, check High Precision Event Timer for how/why things are differently treated according to CPU.
Basically, read the source of your Java and check what your version does with the function, and if it works against the CPU you will be running it on.
IBM even suggests you use it for performance benchmarking (a 2008 post, but updated).
Comment: As all implementation defined bahaviour, "caveat emptor!"
Answer: This doesn't seem to be a problem on a Core 2 Duo running Windows XP and JRE 1.5.0_06.
In a test with three threads I don't see System.nanoTime() going backwards. The processors are both busy, and threads go to sleep occasionally to provoke moving threads around.
[EDIT] I would guess that it only happens on physically separate processors, i.e. that the counters are synchronized for multiple cores on the same die.
Comment: It probably wont happen all the time, but due to the way nanotime() is implemented the possibility is always there.
Comment: The RDTSC counters on multiple cores of the same x86 processor are not necessarily synchronized - some modern systems let different cores run at different speeds.
Comment: I would guess that it only happens on physically separate processors, i.e. that the counters are synchronized for multiple cores on the same die.
Comment: Even that depends on the specific implementation, IIRC. But that's something that the OS is supposed to take care of.
Answer: I am linking to what essentially is the same discussion where Peter Lawrey is providing a good answer.
Why I get a negative elapsed time using System.nanoTime()?
Many people mentioned that in Java System.nanoTime() could return negative time. I for apologize for repeating what other people already said.

nanoTime() is not a clock but CPU cycle counter.
Return value is divided by frequency to look like time.
CPU frequency may fluctuate.
When your thread is scheduled on another CPU, there is a chance of getting nanoTime() which results in a negative difference. That's logical. Counters across CPUs are not synchronized.
In many cases, you could get quite misleading results but you wouldn't be able to tell because delta is not negative. Think about it.
(unconfirmed) I think you may get a negative result even on the same CPU if instructions are reordered. To prevent that, you'd have to invoke a memory barrier serializing your instructions.

It'd be cool if System.nanoTime() returned coreID where it executed.
Comment: All points except 3. and 5. are wrong. 1. nanoTime() is not a CPU cycle counter, it is *nano time*. 2. How the nanoTime value is produced is platform-specific. 4. No, the difference can't be negative, according to nanoTime() spec. Assuming OpenJDK doesn't have bugs wrt nanoTime(), and there are no known unresolved bugs at the moment. 6. nanoTime calls can't be reordered within a thread because it's a native method and JVM respects program order. JVM never reorders native method calls because it doesn't know what happens inside them and therefore can't prove that such reorderings would be safe.
Comment: Your #4 says about negative *difference*, not values: "there is a chance of getting nanoTime() which results in a negative difference."
Comment: Regarding 5. nanoTime() difference results can be misleading indeed, but not for the reasons presented in other points in this asnwer. But rather for reasons presented here: https://shipilev.net/blog/2014/nanotrusting-nanotime/
Comment: Ironically, regarding 6. there had been a bug in OpenJDK specifically due to reorderings: https://bugs.openjdk.java.net/browse/JDK-8184271. In OpenJDK, nanoTime() is an intrinsic and it was permitted to be reordered, that was a bug.
Comment: @leventov, so is nanotime() safe to use? meaning, it can't return negative values and is ~accurate as far as time goes. I do not see the point of exposing an API function that is riddled with issues. This post is a proof, started in 2009 and still commented on in 2019. For mission critical stuff, I imagine people rely on timing cards like Symmetricom
Comment: @leventon - you are wrong about #4. nanoTime() definitely returned negative values in multi-threaded JVM few years ago. that's a fact.
Comment: Yes, nanoTime is safe (since JDK 7 released in 2011, modulo *bugs* in OpenJDK, the last one of which (referenced above) is fixed in 2017).
Answer: Java is crossplatform, and nanoTime is platform-dependent. If you use Java - when don't use nanoTime. I found real bugs across different jvm implementations with this function.
Answer: The Java 5 documentation also recommends using this method for the same purpose.

This method can only be used to
  measure elapsed time and is not
  related to any other notion of system
  or wall-clock time.

Java 5 API Doc
Answer: Also, System.currentTimeMillies() changes when you change your systems clock, while System.nanoTime() doesn't, so the latter is safer to measure durations.
Answer: nanoTime is extremely insecure for timing. I tried it out on my basic primality testing algorithms and it gave answers which were literally one second apart for the same input. Don't use that ridiculous method. I need something that is more accurate and precise than get time millis, but not as bad as nanoTime.
Comment: without a source or a better explanation this comment is useless


SAPUI5 oData POST 500 error

Question: I'm trying to do a oData create on checkbox pressed and getting the following errors. Not sure if this is front end or a back end ABAP issue as have got this same function working in another project.
It is failing on the create part but strangely is still passing through the details for SiteId, ArticleNumber, VarianceDate & Confirmed.

<code>// Set CheckBox status, X for true, blank for false
    onVarianceChecked: function (oEvent) {
        var oEntry = {};

        var bindingContext = oEvent.getSource().getBindingContext(this.MODEL_VIEW);
        var path = bindingContext.getPath();
        var object = bindingContext.getModel("SI").getProperty(path);

        // Pass in the Header fields
        oEntry.SiteId = this.SiteId;
        oEntry.ArticleNumber = object.ArticleNumber;
        oEntry.VarianceDate = moment(new Date(object.VarianceDate)).format('YYYY-MM-DDTHH:mm:ss');

        // Set X or blank
        if (oEvent.getParameter("selected") === true) {
            oEntry.Confirmed = "X";
        } else {
            oEntry.Confirmed = "";
        }

        // Do the create
        var oModel = this.getView().getModel("SI");
        oModel.create("/VarianceHeaderSet", oEntry, {
            success: function () {
                console.log("Variance confirmed");
                MessageToast.show("Variance confirmed", {
                    duration: 1000
                });
            },
            error: function (oError) {
                console.log("Error, variance could not be confirmed");
                MessageToast.show("Error, variance could not be confirmed", {
                    duration: 1000
                });
            }
        });
    }
</code>
Answer: '000000000' is the initial value for Edm.DateTime, hence it will fail when you have modelled a DateTime property to not be nullable.
Go to SEGW and change the property to "nullable" or make sure that you always provide a correct Date in the POST.
Comment: Thanks for your reply. I'll mention it to the ABAP developer tomorrow when  get into work, I'm just the UI5 front end guy :)

We did find a similar answer to this but my ABAP guy was saying that it couldn't be set to nullable as it was a primary key?? Not sure on the why but it's something I'll pick up with him, thanks.
Comment: @Scott key fields cannot be set to nullable.You have to validate the date field in front-end.Or you can send a default date value when passing to backend.

SAPUI5 oData POST 500 error


Why is there so little lighting continuity (consistency) from the close up shot to the wide shot?

Question: In the video from the Joe Rogan Experience #1533, the close up shots of the guests look well lit and perfectly exposed. But the wide shot taken early in the interview around 6:48 looks terrible - the lights are far too bright and the whole shot looks overexposed. What would cause there to be so much difference in the lighting consistency between these two angles?
Answer: The lights are positioned to make the single shots look good.  If you point a load of lights at a camera, the image will look overexposed.
Both these shots are, more or less exposed to make the presenters look correct. But lights visible in a camera shot are much brighter than human faces.
It's a compromise in a small room between which shots are the most important (the close-ups) and the wide is a nice to have.  The alternatives would be bigger softer lights, or indirect lighting, but again with limited space (and budget???) options are limited.
Answer: Yes changing the lighting is probably the best solution but if that's not possible for them they had other options to make it look better.
The reason it looks bad is also due to how the video is processed and choice of camera and the settings used by the camera.
First of all it you can tell that the video has been digitally darkened because the highlights are grey instead of white. Undoing this makes things look a little better. I think the footage could have been graded better.

Second of all the image is overexposed, whoever did this should have reduced the exposure and then maybe brightened the shadows while editing the footage. Another option is to use a camera with wider dynamic range.
Also on another note I think the wide angle shot looks soft. Looking closer I think the issue was noise reduction or maybe it was because of how much YouTube compresses the videos. If it was a result of noise reduction whoever did this if possible should have opened up the aperture of the lens if possible or brought in more lights or if the light brightness is adjustable increase the light brightness.


Functional testing tools like Jameleon

Question: Are there any functional testing tools like Jameleon (in java), available in the .net world?
Purpose I am testing an API written in C#.  I do not want to write unit or integration tests to test them (from QA perspective).  So, I just want to have a custom tool built up, through which I can decide to pass various test parameters and files.
Just for the sake of completion, Jameleon offers Jelly scripts which, like xmls are easy to configure.  These jelly scripts can then be executed like a test case.  Each tag of the jelly script is backed by a Java class which executes the functionality needed.
Answer: Take a look at SpecFlow, it's a Cucumber for .Net
Comment: Thanks for the pointer.  I am actually looking for a tool that also has a GUI to deal with.
Comment: Specflow simply defines what to test, not how to test. You can easily couple it with http://code.google.com/p/wipflash/ or http://white.codeplex.com/

Functional testing tools like Jameleon


Restricted Universal Enveloping Algebras

Question: Is there example of restricted universal enveloping algebra $uL$ of the $p$-Lie algebra $L$ over field $k$ of characteristic $p > 0$ such that $L$ hasn't nonzero $p$-algebraic elements and global dimension of $uL$ is infinite?
Answer: Let $L$ be restricted Lie algebra over $k=\mathbb{G}\mathbb{F}_2$ such that $L$ has generators $x,y,z$ and relations $[x,y]=x^2=y^2=z$. Restricted universal enveloping $uL$ for $L$ has a norm $N$ with values into $k[z]$ and so has no zero divisors. The localization of $uL$ at $z$ is a skew field of dimension 4 over $k(z)$.
Hence there are no 2-algebraic elements into $L$. But $L_1 = L\otimes_k \mathbb{G}\mathbb{F}_4$ has 2-algebraic elements of degree 4 and so trivial module $\mathbb{G}\mathbb{F}_4$ over $uL_1$ has no finite projective resolvents. So $uL$ itself has infinite global dimension. Details on norm are omitted for reader's pleasure (if any).


Deep association and Sphinx index with Thinking Sphinx?

Question: Currently I have the ff table:
Teams
id

Team_User
team_id, user_id

Users
user_id

Item
id
team_id

And the ff. model:
Team
has_and_belongs_to_many :users

User
has_and_belongs_to_many :teams

Item
belongs_to :audit_team, :class_name => "Team", :foreign_key => "team_id"

Why I am trying to archive is to get Sphinx to index all the user_id that associate with Item and I came up with this index definition:
  define_index do
    has audit_team.users(id), :as => :team_users
  end

I am not sure if this is correct or not. Also it's seam that Thinking Sphinx didn't build the config for this. As I rebuild, reindex and run the app, the ff. error shows up:
index ncpar_core: no such filter attribute 'team_users'

What could be the problem and how can I archive this?
Thank you very much
Answer: You may need to run ts:rebuild after adding the attribute so Sphinx is aware of it. Also, perhaps it's a typo here instead of in your code, but id in the attribute definition should be a symbol:
has audit_team.users(:id), :as => :team_users


Does RewriteEngine On enable for all subsequent vhosts

Question: If I have in my conf.d Apache directory 001_vhost.conf 002_vhost.conf ... etc, does Putting RewriteEngine On in 004_vhost.conf enable the Rewrite Engine for 005 006 007 008 etc vhosts, but not those that came before in the virtual host load order?  This is assuming I don't have that directive anywhere else.
Answer: It depends where you have it in the configs (i.e. which section, not which file).
For example, if you have RewriteEngine on within your VirtuaHost section, that will apply to that host only.
If you put it in the global config, that will apply to the main host (not your virtual hosts which need to be enabled individually).
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriteengine
Comment: From the docs "Note that rewrite configurations are not inherited by virtual hosts. This means that you need to have a RewriteEngine on directive for each virtual host in which you wish to use rewrite rules."  That is what I wanted to know, I should have checked there first, thanks.


How good are seedlings at bouncing back after cold weather?

Question: I noticed some of my seedlings (specifically eggplant, and okra) are having trouble due to a few cold days, so I'm wondering how hard it is for seedlings to bounce back after being transplanted from their grow pots to the garden.
Answer: Fruiting garden plants:
If they survives, they will grow again. Just with more delay. Eventually you should remove existing fruits (or the next fruits). Often you can eat them (but not with standard recipes).
Other plants (greens):
The cold may get them the trigger: winter is coming (and passed), so they may skip the first year growth (so good taste), and directly go to the second year: so flower and less green.
Unfortunately you can not do much now, but waiting. Mulch and humidity could help for short cold period. A transparent plastic sheet could also help for the first periods (or as emergency measure). If you have a green house, keeping them in pots helps to have early harvest (and larger plants, so less prone to be killed by slugs).
Answer: Young okra can be damaged by non-freezing cold temperatures, much as watermelon. However, it's not a sure thing that only a few days of cold will kill them (it might set them back and/or stunt them for a while), and it might impact germination, but I'm guessing they'll live. You might want to give them some phosphorus (it's said to help with cold tolerance, and plants tend to show deficiencies in phosphorus when it gets cold; I usually need to give my plants phosphorus in my unheated greenhouse if I use a seed-starting mix).
Eggplant should be fine, I'm guessing. It seems slightly more tolerant of cold than tomatoes, and tomatoes are usually fine (although some breeds of tomatoes definitely do better in the cold than others, and the same is probably true for eggplant). I've never had problems with eggplant in my unheated greenhouse. I have had problems with okra, though (and although it can initially sprout fast in the greenhouse, the cold can stunt or kill it—the cold can also inhibit germination).


What is the origin of the "Dear John" letter?

Question: You might receive one of these when you are getting dumped by your erstwhile partner.  I don't think it is a coincidence that the a recent movie called Dear John includes a Dear John letter as one of its plot points.
Comment: Ngram shows the origin to be roughly [1943](https://books.google.com/ngrams/graph?content=dear+john+letter&case_insensitive=on&year_start=1800&year_end=2000&corpus=15&smoothing=3&share=&direct_url=t4%3B%2Cdear%20john%20letter%3B%2Cc0%3B%2Cs0%3B%3BDear%20John%20letter%3B%2Cc0%3B%3BDear%20John%20Letter%3B%2Cc0%3B%3Bdear%20John%20letter%3B%2Cc0).
Comment: I also got some hint that the Dept of the Navy sent some officers a letter -- presumably "your services are no longer needed" -- at the end of WWII, and these were referred to as "Dear John Letters".  And "Dear John Letter" was a popular country music song in 1953.
Answer: According to Wikipedia:

While the exact origins of the phrase are unknown, it is commonly believed to have been coined by Americans during World War II. Large numbers of American troops were stationed overseas for many months or years, and as time passed many of their wives or girlfriends decided to begin a relationship with a new man rather than wait for their old one to return.
As letters to servicemen from wives or girlfriends back home would typically contain affectionate language (such as "Dear Johnny", "My dearest John", or simply "Darling"), a serviceman receiving a note beginning with a curt "Dear John" would instantly be aware of the letter's purpose.
A writer in the Democrat and Chronicle of Rochester, NY, summed it up in August 1945:

"Dear John," the letter began. "I have found someone else whom I think the world of. I think the only way out is for us to get a divorce," it said. They usually began like that, those letters that told of infidelity on the part of the wives of servicemen... The men called them "Dear Johns".

Here's another source that concurs:
http://www.worldwidewords.org/qa/qa-dea5.htm
Comment: Wow. That is the single saddest answer I have ever seen on stackexchange.
Comment: This is also said to be the origin of the expression "That's all she wrote."
Answer: Early in Anthony Trollope's 1864 novel "Can you Forgive Her?" the heroine, Alice Vavasor, writes a letter that is central to the novel's plot development. She breaks off her (second) engagement by writing a letter, addressing her fiancee "Dear John" and then repeating this same greeting several times throughout her letter. So I imagine this very popular London novel was the origin of the concept, and that the idiom was further popularised in the USA during the second World War as suggested.
Answer: Here is more information about "Dear John" from Wikipedia:
"A Dear John Letter", or "Dear John" is the name of a popular country music song. It was popularized by Ferlin Husky and Jean Shepard, and was a crossover country-pop hit in 1953.
The song played on the concept of a Dear John letter while referencing the United States' involvement in the Korean War. Here, a young woman, whose boyfriend John was stationed in Korea, writes stating that she is breaking off the relationship. To add to the heartbreak: She is marrying John's brother, Don, and wants her photograph back (because — according to the lyrics — Don wants it now).
If you want to know more about the song lyrics here is the link:http://www.lyricsmode.com/lyrics/f/ferlin_husky/a_dear_john_letter_lyrics.html


Monopoly: Why is it this way?

Question: Is there any real-world reason why someone absolutely has to spend a night at a $2000 hotel when they have five dollars in their bank account, or is it just "the rules"?
Comment: What happened in October 1929 was that "rich" people stayed in hotels, the stock market crashed, they lost all their money (because they bought on "margin") and they jumped out of the hotel windows. Shortly afterward, there was a "joke" that when someone booked a hotel room, the clerk would ask him, "for sleeping or for jumping?
Answer: Monopoly was originally designed as a game to show the effects of capitalism. But the people liked the collection of money and the suffering of their fellow players too much so the original purpose was soon lost.
The game is a model of a subset of reality. So yes it needs rules. They may seem unfair, but real life can be a bit less optimal. And at least with this game, you can flip the board, curse at your (obviously cheating) little brother and storm out of the room. Real life has some limitations on that.
Comment: Although I generally like the answer you gave, I believe you should review your language.
Have you consider to also attach some links backing up your points? That would help a lot.
Answer: An obvious real-world reason is that hotels charge you for your stay whether you have the $2K or not.
The actual site of breaking the "real world" connection is that a (reasonable) person with $5 in the bank would stay home, yet the person locked in the body of a top hat or scottie dog is not permitted that option on the game board.
These sorts of concessions are part and parcel of board games. The simplification of the simulation is needed for the game to be both rigorously defined and still playable.

Monopoly: Why is it this way?


IMAP Email Sending Failure

Question: In Email, I have 3 accounts, 2 x hotmail and 1 x IMAP. Since the last update (188.8.131.52) where it demanded I give Email access to the Phone, I have not been able to send any emails from the IMAP account. The Hotmail Accounts are both fine. Samsung S7. Can anyone help me?
Comment: I have a couple of questions reading this: What app are you using for email? I'm not familiar with one called "Email" in stock Android. Did all three accounts work fine before the update? What happens when you try to send from your IMAP acct? Is receiving also broken or just sending?   I recommend editing your question (rather than just replying to my comments) so that others can better help you troubleshoot.
Answer: Go to Mail - Settings - Accounts and re-input all of the settings, one may be corrupt even though it looks fine.

IMAP Email Sending Failure


Battery died at a drive-in and the guy that jumped off the car with a jump box hooked a jump box up backward

Question: After you hit the jump box up backwards I attempted to start the car twice before I realized that he hooked it up backwards it is a 2011 Toyota Camry now the car starts but it has no acceleration at all and it has a check engine light on but I have no way to get it to get the light checked
Comment: Welcome to Motor Vehicle Maintenance & Repair!
Answer: Unfortunately, something was damaged during the jumper box mishap. The big things being the alternator or the engine control unit (ECU). Since you can still start it, I'd suspect these are probably still in working order. The battery was recharged after the "successful" jump, which means the alternator is working. And since it will run, the ECU is at least attempting to work.
There's not a whole lot you can do until you can figure out what the codes are behind the check engine light. One thing you can do is to check fuses. There's probably two sets of fuses. I'd just go through and methodically check each one. If you don't have a digital multimeter to check them in their slots, you'll need to carefully pull each one and see if they are blown, replacing any which gave up the ghost.


Will using my router as an AP cause slow file transfer on the network?

Question: Will using a second wireless router as a wireless AP LAN-LAN cause a bottleneck at the LAN port, since my router is capable of wireless transfer speeds of up to 300 Mbps and my LAN port is only 100 Mpbs? I do a lot of file transfers between devices on my network that are only reached by my second AP. Would it be better to create a separate network and have it wired LAN-WAN or keep my current setup of LAN-LAN? I have four devices that connect to the AP, and about 15 devices that connect to the primary router. I am not concerned about the devices connected to the first router communicating with devices connected to the second router.
Answer: You cannot, ever, reach 300 Mbit/s net speed on 11n WiFi. It's usually about half of it—150 Mbit/s.
Of course, this is still more than 100 Mbit/s. So yes, any WiFi-LAN transfer will be limited to 100 Mbit/s. WiFi-WiFi transfers are not subject to this limitation.
I don't quite get the next part. You better draw some graphs showing what you mean. ;)
Of course, you cannot work around the WiFi router's limitations. To get full 11n speed, you'd need a Gigabit-speed wired network.
Comment: Which is the reason you buy hardware that supports 10/100/1000 speeds
Comment: My setup right now is temporary until I move, my secondary router supports 10/100/1000, but my primary router does not. To clarify the second part of my question, if my second router is just acting as an access point, will WiFi-WiFi transfer have to travel back to the first router since my secondary router is not acting as a DHCP server, or will WiFi-WiFi transfers only go through the second router?
Comment: @Steven No. DHCP servers do not participate in file transfers. In fact, you don't need a DHCP server at all. Of course, you'd have to manually manage IP addresses in that case.


How to setup debug log for batch apex

Question: I have a batch 'mytestbatch' which runs everyday through scheduler. This batch will update more than 15lakhs records. During execution mytestbatch is throwing an error 'First error: Attempt to de-reference a null object'. I know this error is coming might be due to some bad data or some null values during an execution. I need to know from which line this error is coming. Under setup I have set the debug log  for an user on whose name mytestbatch is running. But not able to capture the error due to exceeding debug log limits as more records are getting updated in this batch.
Can anyone tell how to capture this root cause of this error?
Answer: There are basically two options open to you:

Ensure that you set up a Debug Levels definition that minimizes most of the captured data, as covered in the documentation, whilst definitely capturing at least ERROR level for Apex. Indeed, for the issue you face you can probably set everything to just ERROR. Once you set up the debug levels, use it on the trace flags you create for the batch's execution user.
If you need more complete information you can still have more captured data if you can change the batch's scope chunk size, e.g. to 1. This means you will get a separate debug log per record processed by your batch. Depending how you wrote the code that calls Database.executeBatch this may require a small code change.

It may be that you could manually invoke the batch through anonymous apex, in which case you can simply set up the SFDC_Console debug level, open the developer console, access the Execute Anonymous Apex dialog and explicitly call the execute batch. The console debug level is automatically applied to the user you opened the developer console as.
Comment: Thanks Phil. I will do what you said and let you know as this batch runs daily for more time. So I need one day to test this.
Comment: Like I said, you could run it manually through the developer console, assuming this is easy to construct an instance of the batch...
Comment: Yep but this batch is already scheduled in Prod. So I don't want to disturb that. Let it run in scheduleded time
Answer: You can send email in execute method like this,
whenever there is any error it will send email :
<code>global void execute(Database.BatchableContext bc, List<sObject> scope) {

    try{
       //process data here
    }
    catch(Exception e) {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {'your email address'};
        mail.setToAddresses(toAddresses);
        mail.setSubject('Error in mytestbatch ' );
        //You can add more detail about error from Exception object
        mail.setPlainTextBody(e.getStackTraceString());
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }

}
</code>
Comment: Thanks Kritik. I have this batch class schdueled in Prod So only I'm not able to incorporate any email sending methods or automatic case generations for error.
Answer: I got the required error log through.....
I have created a new debug level (Apex code,system,database Finest and other are None)and created a debug log for an user on whose name the batch is running.
As soon as batch starts runs then parallelly I keep deleting the success logs. In this I'm not getting hitted with any limits. I know its quite difficult due to continuous monitor and human effort. But when we are failing to implement error capture method then we have to suffer like this only. Good to keep error capture method in all classes.

How to setup debug log for batch apex


How can I add a script to embedded Linux

Question: I have a script (let's say Hello World), that I want to include to my embedded Linux. I'm using Buildroot and want to start the script in Busybox. But because I'm cross-compiling for the target I'm not sure where to put that script, that in the end it's gonna be compiled and can be executed in Busybox.
Answer: a shell-script does not need to be compiled, in order to be run: the nature of scripts is to be run directly by an interpreter (your shell-implementation).
hence, you only need to put the script into a place where it will be found by your shell. for administrator-installed scripts, the preferred place is /usr/local/bin (or /usr/local/sbin if the script is to be run in "administrator mode").
make sure that the script has proper permissions.
# cp /path/to/script/hello_world.sh /usr/local/bin
# chown root.root /usr/local/bin/hello_world.sh
# chmod a+x /usr/local/bin/hello_world.sh
# hello_world.sh
Hi there!
#

Comment: yes i know. But where to put that script, that next time when i hit "make" I'm sure it's included in the generated image for the target?
Comment: this depends on your `Makefile`.
Answer: If you want the script to run at start up and you are using the busybox init process in your system then place a script in the /etc/init.d directory.
You need to make sure that your script file name is of the form
SXFilename

where X is some integer that specifies the order that it is executed in - the lower the X value the earlier that the script will run. Depending on how you configured buildroot you should see some scripts similarly named already in your /etc/init.d directory. I am assuming above that you have not edited your inittab file to disable the running of these scripts of course.
Cross compiling is not an issue at all since the script is not compiled and is just executed using ash or bash if you have chosen to build bash package into your buildroot setup.
Comment: No I just wanted to have the script on my target, to make it run whenever I want, not only as init. And so I have to cross-compile the image (!) where I included my script as part of the file system.
Answer: I've found there are several ways to do so. I created an fs-overlay, hit make, and it was part of the Image.
Thanks for the advice.


Delegate External User Administration functionally (Partner community Users)

Question: Delegate External User Administration functionlly not working i have assign one user as a delegate admin to other Profile users. when  i login as a Delegated admin user in Community i am not able to see the Manage External Users Button on other users. So that i can change password or deactive users.
Any help will be appreciated. Thanks
Answer: "Manage External Users" button is only available in classic for internal users.
In Lightning, as a delegated external user admin, you should have "Enable {Partner, Community} User".


How best to block ads on non rooted phone?

Question: How can you block ads on a non rooted phone. I tried rooting and have no success.
Comment: See Izzy's list of [ad blockers](https://android.izzysoft.de/applists/category/named/network_adblocker). I use Adguard, it's excellent but paid. You can see other alternatives in the list
Comment: Ill look for its alternatives
Comment: Hotwav hot 5th is what i was working with
Answer: Adguard is a good option (as suggested above) given it can employ more than one way of filtering ads from traffic. It can utilise local VPN or local HTTP proxy, but can have significant battery drain.
A more lightweight option may be using DNS66, an open source application available for free on the F-Droid Repository.

DNS66 uses DNS level filtering to filter ad traffic. This means that
  it only filters the ad traffic for a specific amount of time after
  your device is connected to the internet. Therefore there will not be
  any significant battery drain when you are using DNS66 to block ads
  across the system of your Android device.

After installing, head to the Hosts tab at the top of the screen. From here, you'll have to pick at least one ad-blocking hosts file, which is basically a list of known ad servers that DNS66 will block for you.

Now you have to select the hosts file which contains the list of popular ad servers that are hosting the ads you see across the websites and apps.
The most recommmended one is  Adaway hosts (as it covers most popular ad servers) and then click on the Action option and select deny. This will now block all the ads that are hosted by the servers in the Adaway host file's list.
From here, head back to the Start tab from the top menu, then tap the "Start" icon near the bottom of the screen to start the VPN ad-blocking service.
acknowledgements

Block Ads in All Your Android Apps Without Root or Extra Battery Drain
Answer: If you wish to keep your phone not rooted you still have several options.

use ad block apps on your phone
install AdBlock chrome extension for android
configure your router to block specific sites (not all of them has this feature)
deploy your own proxy to block what you want

I've been in similar situation before and figured out that the best option for me it the 4th one. So I created a pre-configured squid image deployed in docker on raspberry pi, opened the port on my router and set RPI external ip address as proxy. Since then I have no ads on my phone, not on any site not in the apps and I don't have to worry about privacy because I manage the proxy.
The docker image, its source code and a short documentation are available here: https://github.com/andrassebo/squid-adblock
Comment: Curious about "install AdBlock Chrome extension for Android", considering that Chrome for Android doesn't support browser extensions.
Comment: Thanks for pointing it out, it's not a traditional extension (like on PC). But here is one from many: https://play.google.com/store/apps/details?id=org.adblockplus.adblockplussbrowser


I have evidence of inequitable conduct for an issued US Patent - What are my options?

Question: http://proceedings.spiedigitallibrary.org/proceeding.aspx?articleid=971777
The above article predates the patent US5576880.  I sent this article to the inventor of US5576880 prior to his filing of this patent.  He submitted this patent application on the paper and failed to notify the patent office of this prior art.
Comment: We now use the term "inequitable conduct". Are you alleging that the inventor had knowledge of highly relevant prior art and did not properly inform the examiner? Until a few years ago that was getting patents torn up due to inequitable conduct. The Therasense case changed the standard. Now it is much harder to establish. Also, people have no obligation to open everything they get in the mail (or email). Due to its age is not easy to look at the record to see if that ref. was cited by the applicant but it is not on the face of the patent. Or do you think you are the true inventor?
Answer: Inequitable conduct is primarily a defense to an infringment accusation.
One option you have is to submit that paper under 35 USC 301.

35 U.S.C. 301  Citation of prior art and written statements. (Sept. 16, 2012)
  (a) IN GENERAL.—Any person at any time may cite to the Office in writing—
  (1) prior art consisting of patents or printed publications which that person believes to have a bearing on the patent ability of any claim of a particular patent; or
  (2) statements of the patent owner filed in a proceeding before a Federal court or the Office in which the patent owner took a position on the scope of any claim of a particular patent.

You would not be able to say that you know the person had access to the paper in advance or allege inequitable conduct. But the paper would be sitting in the file as something that might be looked at for its pure prior art value in the case of reexamination, reissue or litigation.
Or under 35 USC 302 you can file requesting ex parte reexamination based on that document's pure prior art value. The least that would cost is $6000 in USPTO fees assuming the requestor qualifies as a small entity.
Or you could file an inter party review or a post-grant review under the new AIA procedures. I do not believe that either inequitable conduct or untruthful declaration of inventorship can be raised in either of these, more expensive procedures.
Of course, your tagged posting here at Ask Patents might be seen by a future infringement defendant.


My car speaks occasionally after it starts

Question: kind of a weird one. Sometimes my 2008 Honda Civic EX quietly speaks words after it starts. I've disconnected any audio devices and it still speaks. As far as I can tell, it's speaking French; I can kinda catch a "s'il vous plaît" at the end of whatever it says. I can't find anything online about this, and it's been driving me up the wall.
Comment: It's not a Bluetooth message, which was n/a until 2009 on the Civic. Does it speak if you fasten your seat belt before starting the engine?
Comment: No, but it also doesn't consistently speak when I don't fasten til after
Comment: So if you always remember to fasten your seat belt before starting, you will never hear the voice again? That sounds like a way to stop being driven up the wall. Confirmed? If so I'll write that in an answer.
Comment: Well no, I haven't fastened my seatbelt enough times before starting the engine to know whether it stops it from speaking.
Comment: I think the only way to know for sure, is if you recorded some audio while starting, and turned off the radio/fan/AC to avoid any extra noises
Answer: In comments you've said that the car didn't speak when you fastened your seatbelt before starting the engine, so I'm assuming that this is a "please fasten seatbelt" message.
You also said that you haven't tried this enough times to know if this always works.
So I suggest that you always fasten your seatbelt before starting the engine if you don't want to hear the message. And if it turns out that the car still talks, come back and post a new question.


get_terms custom order

Question: I am using the get_terms function to order all the terms of a custom taxonomy alphabetically. My client however has now requested that one of the terms be put at the very end (therefore not in alphabetical order). Does anyone have any suggestions on how the best way to achieve this?
Comment: possible duplicate of [Order get\_terms() By Custom Field](http://wordpress.stackexchange.com/questions/117147/order-get-terms-by-custom-field)
Comment: @Howdy_McGee sort of, but Duncan didn't say anything about having any custom field values for his terms
Comment: How can this be a duplicate post. Yes, custom fields can be an option, but it is **not** the only option. What if the OP don't want to use custom fields.
Answer: This can be easily done with your own custom function. What you want to do here is, get your object from get_terms() which will hold your term objects, check if your unique key is set as a parameter and then according to that, remove that key/pair and add it to the back
Now, lets put that into code: (I have commented the code to make it easy to follow)
This is the pre PHP 5.5 version but still need at least PHP 5.4
function get_term_to_end( $taxonomy = '', $args = [] )
{
    $terms = get_terms( $taxonomy, $args );

    /*
     * Return $terms if our new argument term_to_end is not set or empty
     */
    if ( !isset( $args['term_to_end'] ) || empty( $args['term_to_end'] ) )
        return $terms;

    /*
     * If term_to_end is set and has a value, continue to process
     * Return $terms if a wp error is returned or if $terms is empty
     */
    if ( is_wp_error( $terms ) || empty( $terms ) )
        return $terms;
     /*
      * We have came this far, now we can finish this off
      */
    foreach ( $terms as $term ) {
        /*
         * Check the term ids against our new argument term_to_end
         */
        if ( $term->term_id == $args['term_to_end'] ) {
            $end[] = $term;
        } else {
            $terms_array[] = $term;
        }
    }
    /*
     * Merge the two arrays together, adding the term_to_end right at the back
     */
    $terms = array_merge( $terms_array, $end );

    /*
     * For uniformaty, type cast the resultant array to an object
     */
    return (object) $terms;
}

If you are using PHP 5.5 +, you can use array_search and array_column and skip the foreach loop inside the function
function get_term_to_end( $taxonomy = '', $args = [] )
{
    $terms = get_terms( $taxonomy, $args );

    /*
     * Return $terms if our new argument term_to_end is not set or empty
     */
    if ( !isset( $args['term_to_end'] ) || empty( $args['term_to_end'] ) )
        return $terms;

    /*
     * If term_to_end is set and has a value, continue to process
     * Return $terms if a wp error is returned or if $terms is empty
     */
    if ( is_wp_error( $terms ) || empty( $terms ) )
        return $terms;

     /*
      * We have came this far, now we can finish this off
      *
      * We need to convert the multidimensional objects to a multidimensional array. Lets used
      * json_encode and json_decode. Now we can use
      * array_search to and array_column to determine the position of the term_to_end
      */
    $terms_array = json_decode( json_encode( $terms ), true );
    $end_term_position = array_search( $args['term_to_end'], array_column( $terms_array, 'term_id'));

    /*
     * First check if $end_term_position in not false (array_search returns false on failure),
     * if false, return $terms
     */
    if ( !$end_term_position )
        return $terms;

    /*
     * Get the key value pair for term_to_end, unset it from $terms and reset term_to_end pair at the back
     */
    $end_term_pair[] = $terms[$end_term_position];
    unset( $terms[$end_term_position] );
    $new_terms_array = array_merge( (array) $terms, $end_term_pair );

    /*
     * We are done!! Now we can just return $new_terms_array as an object
     */
    return (object) $new_terms_array;
}

Example use case:
Move term with id 15 belonging to the taxonomy mytax to the back of the term array. Keep the default odering by name
$terms = get_term_to_end( 'mytax', ['term_to_end' => 15] );

Or
$args = [
    'term_to_end' => 15
];
$terms = get_term_to_end( 'mytax, $args );

FEW NOTES ON THE ABOVE

The code above is all untested and might be buggy. I have written this on a tablet.
For older versions (pre PHP 5.4), use the first code block and simply change the new array syntax ([]) to the old syntax (array())
The new parameter, term_to_end uses the term to move to the end term's ID. You can modify this to work with either the term name or term slug, whatever suites your need. You can also modify this to work with all of these values
The new function uses the exact same parameters as get_terms(). The only extra addition is the term_to_end parameter
As get_terms, the new function returns a WP_Error object if a taxonomy does not exists and also returns an ampty array if no terms are found. Remember to check for these occurances in the same way you would with get_terms
If the term_to_end parameter is set, the fields parameter cannot be set to anything else than all (which is the default). Any other value will break the function. You can modify this very easily to work properly by building in some coditional checks and changing the foreach in the first code block or changing the use of array_search in the second code block
You can very easily change the function to move a selected term to the front if needed. You can also adjust the function to accept multiple terms to move
For any other info regarding the use of this function, please see get_terms

EDIT
This code in now tested and working as expected.
Comment: MY pleasure, glad it works. Enjoy :-)
Answer: There may be a better way of doing this, but you could probably just do two queries. Maybe something along these lines (untested):
$loner_term = X; //put whatever the id of the term you want at end
$taxonomies = 'YOUR_TAXONOMY_NAME';
)
// get all of the alphabetical terms and exclude the one you need at the end
$alpha_args = array(
    'orderby' => 'name',
    'exclude' => $loner_term
);
$alpha_terms = get_terms($taxonomies, $alpha_args);

// only get the term you need to come at the end
$na_args = array(
    'include' => $loner_term
);
$na_terms = get_terms($taxonomies, $na_args);

echo '
';
// loop through the alphabetical terms
foreach ($alpha_terms as $alpha_term) {
    echo '
'. $alpha_term->name.'
';
}
// then loop through the loner term
foreach ($na_terms as $na_term) {
    echo '
'. $na_term->name.'
';
}
echo '
';

EDITED to use get_terms properly!
Comment: You can do this in one query
Comment: Just another note here, please recheck your code, it is extemely buggy. You are feeding arguments to `get_terms` that does not exist, and also, you are using `get_terms` incorrectly. The syntax is `get_terms( $taxonomies, $args )`, not `get_terms( $args )` :-)
Comment: @PieterGoosen ahh just logged on to see what the latest with this was and it looks like I should have done so earlier. I was mainly trying to throw the idea of two queries into the ring, but definitely should have taken a bit more time on it.
Answer: I stumbled across similar issue lately. If you are comfortable with using a plugin, you can install Custom Taxonomy Sort. Even though it was not updated for over two years, it solved the problem for me. Basically, it allows you to sort terms manually from the dashboard.
Comment: The only problem with this is that the user would have to update the order whenever they added a new term, which could definitely be a pain depending on the number of terms.
Comment: Makes sense. In my case, there were only 12 terms that don't change over time.
Comment: Ah yeah. Another one I've seen for the same thing is: https://wordpress.org/plugins/taxonomy-terms-order/
Answer: An idea that came to me is to add a term in your taxonomy called something like 'append' and then make the terms you want to be appended at the end of your terms list children of that term. Then it is only a matter of doing the appropriate term queries, ie get all terms with append as it's parent and then exclude them when quering for the rest. This way you maintain editorial control to this custom ordering thing and don't have to hardcode anything into your template.
Comment: Yes, it is an idea, but what if the user has specific terms, say like ingrediends or states which needs to be just that. Just a thought :-)
Answer: // Define a custom order for category IDs
$custom_order = array(
    13, // Category ID for 'featured'
    51, // Category ID for 'all-resources'
    21, // Category ID for 'resource-type'
    19, // Category ID for 'service'
    22  // Category ID for 'industry'
);

// Retrieve terms (categories) with specified parameters
$categories = get_terms('category', array(
    'parent' => 0,                // Get only top-level categories
    'exclude' => $excluded_categories, // Exclude specific categories, if needed
    'include' => $custom_order,   // Include categories in the specified custom order
    'orderby' => 'include',       // Order categories based on the custom order
    'hide_empty' => false,        // Include categories with a count of 0 (no associated posts)
));

get_terms custom order


How well is the HFS+ filesystem supported?

Question: How well does Ubuntu support HFS+ (i.e. Mac formatted) filesystems? Can it read and write to them, and are there any potential problems to watch out for?
Answer: The kernel supports basic read support - however, journaling support is nearly non-existent which is needed for write support. By default most modern Macs (which utilize the HFS+ Filesystem) have Journaling enabled. For write support you'll need to first disable journaling on the HFS+ Partition:
sudo diskutil disableJournal [VolumeName]
Then, if needed, re-enable with:
sudo diskutil enableJournal [VolumeName]
However, as a warning - Journaling is an important part of any Filesystem which implements it. Disabling it in Mac OSX is not recommended and  can have adverse affects. There has been work to include journaling support for the HFS+ driver in the kernel - but that it still some time away.
Comment: Do you mean the HFS+ driver in the linux kernel doesn't work if journaling is enabled in Mac OS X?  Or do you mean the linux driver will ignore the journal (which would be the same as what ext2/3 drivers for Windows do currently AFAIK)?
Comment: @JanC You can't write without Journaling support. So if Journaling is enabled on an HFS+ partition you will only have a read-only mount. EXT2 does not have Journaling, and the driver for Windows to access EXT3 has Journaling support included in it (since it's been around since 2001)
Comment: The ext2/3 driver for Windows I have used ignored the journal; it only supported ext2, and ignored the ext3 features, just like you can force-mount an ext3 as ext2 on linux and ignore the journal.  So, good to know this is different for HFS+.
Answer: SuperUser has a great answer for mounting HFS+ partitions in read/write mode here:
https://superuser.com/questions/84446/how-to-mount-a-hfs-partition-in-ubuntu-as-read-write
Answer: Looking at the HFS+ driver in the kernel, it seems to have read & write support, but I have absolutely no idea how stable/useful/etc. it is...


What is the state of C++ refactor support in Eclipse?

Question: Is it at the state where it is actually useful and can do more than rename classes?
Answer: CDT (C/C++ Development Tools - eclipse project) 5.0 has a bunch of new refactorings
<code>* Declare Method
* Extract Baseclass
* Extract Constant
* Extract Method
* Extract Subclass
* Hide Method
* Implement Method
* Move Field / Method
* Replace Number
* Separate Class
* Generate Getters and Setters
</code>
There is a CDT refactoring wiki
Answer: There have been numerous efforts to provide refactoring tools for  C++, most of them failed pretty early, because the creation of such tools requires the full ability to process C++ source code, i.e. you need a working and full c++ compiler in the first place to implement even the most basic forms of automated source to source transformations.
Fortunately, with the introduction of plugins into gcc, it it's finally becoming foreseeable that related efforts may actually be able to leverage an existing C++ compiler for this purpose, instead of having to resort to their own implementations of a C++ compiler.
For a more in depth discussion, you may want to check out this.
For the time being, the most promising candidate to provide widely automated C++ refactoring support, is certainly the Mozilla pork project, along with its related companion project Dehydra.
Answer: Some C++ refactorings which are supported by for example by Ref++ do not need to fully understand C++ syntax. For example pull up method, push down method etc are quite straightforward. For some reason this kind of refactorings are not implemented to CDT refactorings.
Answer: Yeah and most of them don't work actually if the code is too complicated. Things like move a method, rename, etc have problems sometimes.
Comment: don't use macros and #if and all is going to be fine.
Answer: C++ is a very hard language to provide refactoring support for. This is because the langauge is very complex and hard to parse but its mostly because of the preprocessor.
The preprocessor is the main reason why C/C++ IDEs lag behind other languages.
Comment: That, and the fact that C++ does not have a stateless grammar.


Master-Detail view in combination with ORM, Unit of Work and Repository patterns

Question: I am unsure on how to best implement a master detail view in combination with a ORM.
The application uses WPF with MVVM and displays the grid of all available objects as well as the details of the currently selected object.
The ViewModel of the view is pretty simple, it has:

An ObservableCollection<ItemViewModel> Items where ItemViewModel is a ViewModel for the domain object that should be displayer in the view. This property is bound to the grid.
A ItemViewModel CurrentItem property that is bound to the grid's SelectedItem.
ICommand for "Add new item", "Delete selected item" and "Save changes to selected item".

My application uses NHibernate as its ORM tool but I don't want to leak NHibernate throughout the code base so I abstracted it away using the "Unit of Work" and "Repository" patterns.
The NHibernate specific implementation of these patterns is such that a "Unit of Work" opens a new NHibernate session and transaction at the start and at the end commits the transaction and disposes the session. So, the lifetime of the Unit of Work, the session and the transaction is the same. The Unit of Work has a property Repository that is using the same session as the Unit of Work and also has the same lifetime.
This is where it is getting problematic:
I would like to populate the Items collection in the constructor of the MasterDetailViewModel. Currently, I would need to create a new Unit of Work and populate the collection. To not have long running transactions, the UoW would be disposed directly afterwards, disposing also the underlying session.
Now, when the user wants to save changes to the current item, I would need to open another UoW, retrieve the entity from the database, update it with the current values of the ItemViewModel, save it to the database and dispose the UoW.
However, this approach has several important draw backs:

My code is littered with using(var uow = uowFactory.StartNew()).
No optimistic locking is taking place. If someone else would have changed the same item in the database, his change would silently be overwritten.
It requires two database hits to update the item instead of one.

This leads me to the conclusion that the implementation of my UoW is flawed.
I thought about changing the implementation so that the relationship between UoW and the Repository is reversed. This would mean that the IoC would inject a Repository into the ViewModel instead of an UnitOfWorkFactory. The Repository now is a functional equivalent of NHibernate's ISession. The Repository can start a new UoW which now is identical to a database transaction.
This would go well with my Master/Detail scenario but it doesn't support the notion of a "business transaction" that normally is used as a synonym for Unit of Work, e.g. a logical transaction spanning multiple database transactions and user request.
The question is: How to best implement the two patterns Unit of Work and Repository to be usable in both scenarios?
Comment: i have not used the abstractions but i opened a long running session which populates the grids and evicts all items which are not displayed anymore (except a frame around the displayed items) and use transaction.Begin(), transaction.Commit() around each buisiness operation "add new", "change" and the like.
Comment: @Firo: Thanks for re-assuring me that a long running session is a good thing in that case.
Answer: Changing the place where you start a business transaction doesn't necessarily change its granularity.
Not sure I completely get your example, but I can't see how starting a new UoW in each of the Repository's methods (each time it queries the persistent store) is so much different granularity-wise than starting a new UoW when the MasterDetailViewModel populates its items, and another one each time one of the items is saved.
However, as you suggest I would inject the Repository in the ViewModel itself since it seems odd for the UoW to have a Repository property - the UoW doesn't manipulate a Repository directly, it's rather some object that uses the Repository in the context of a UoW.
Other than that, I think you pretty much nailed the drawbacks associated with short-lived, fine-grained business transactions. There's not much you can do about it.
There are plenty of other session strategies though. You'll find some suggestions in this article.
Also see
What is your session management strategy for NHibernate in desktop applications?
What should be the lifetime of an NHibernate session?


Why most of the microphones are placed on the left of headsets / headphones?

Question: I've noticed that the mayority of the well-known brands (Sennheiser, Logitech, Plantronics, Razer, SteelSeries, Audio-Technica, Phillips, etc) tend to make headsets with the microphone on the left, is there any UX related reason for that tendency? If not, what's the reason of that choice?
The position of the mic would be equidistant to the the mouth when placed on the right side, so there's no difference when recording.
My improbable theories:
1) Maybe it could be useful for people who eat (or make something using with their right hand and mouth, like smoking) while wearing their headphones, supposing that the person is right-handed(what it's true in most cases) and the mic is neither in front nor too close to their mouth.
2) I think we usually align our body more to the left side of the keyboard (centered between 'g' and 'h' keys or so, and probably to center of monitor too, to avoid neck tension) for writing comfortably, and right-handed ones place the mouse on the right, so the mic on the left could go along with the "left side weight" and avoiding the possible sensation of the microphone "being in the middle".
This theory is enhanced in the case that the cable is not divided in two to reach each side and just reach the side where the microphone is placed, so the cord will hang closer to that side and not centered to your body.
Comment: rewobs, as worded right now this question is likely to generate a lot of discussion and not many useful answers.  Is there a specific problem you are facing regarding the placement of microphones on headsets?
Comment: Apple earbuds, one of the most-worn headphones [citation needed] with mic have it on the right.
Comment: I've been wearing my Plantronics headset with the mic on the right. I just now noticed I can wear it either way. Maybe some of your data points are ambidextrous headsets that people have chosen to wear to the left? (I'll switch to left - I keep bumping the mic when I go to adjust my glasses with my right hand.)
Comment: Theory #1 sounds fairly solid. Wires get in the way. Most people are right handed. Put the wires on the left to keep them out of the way.
Comment: my headphones have mic on right. haha.
Comment: It is an industry standard to feed the cables from the left side, maybe that includes the microphone? I mean, given that the mic wire would have to run extra distance if it were placed on the right, it would make sense for it to be on the same side as the rest of the wires.
Comment: @msparer Beyond how many people use them I was asking if the decisions that manufacturers do have any UX related foundation. I edited it to make it clearer. Here apple products are not common, anyway I thought mics on apple earbuds were attached on the cord single cord part and not on any left or right division.
Comment: All headphones with mic I have used have the mic on the right side.
Comment: @JoshuaBarron As an engineering student and UX lover this question just came up to my mind and although it is not on my plans to start making headphones, I wasn't going to wait until that to get a possible answer. I've researched it for a while on the web without success, and because of possibly being related with UX design, I thought it was okay to ask it here and not just in a technical forum. I'm aware that it can bring up some discussions but I don't see why it couldn't have an objective and non-opinion based answer. I edited the question and added some text to make it a bit more precise.
Comment: With every headset I've ever had, the mic can be rotated through somewhere around 300' so that you can wear it with the mic on whatever side you like. Personally I wear mine to the right and am left handed if that makes any difference.
Comment: So you can hold a phone up to the right side of your head with your right hand? Not that anyone would ever do that. Some headsets that include a mic have only one earpiece... Hmm.
Answer: The cord on headphones is usually on the left, so it makes sense for the mic to be on the left too so that the mic's cable doesn't have to cross all the way over to the right side, thus reducing the wiring/complexity. That's my guess anyway.
Why the cord is on the left, I don't know.
Answer: My thinking is along your theory #1.
As some people have commented, the microphone can also be on the right side, which indicates to me that this isn't an absolute design standard. It seems to be more convenience based then. So what is convenient about it?
As stated in your first theory, it frees up space, allowing for uninterrupted interaction with the face. People naturally eat using their dominate hand, related. With the mic on the non-dominate side, it doesn't impede eating/drinking. I see this being one of the largest factors.
From personal experience, I find that when using a headset, it's dependent on the situation I am in whether or not it being on one side or the other is better. If I am at my computer, I use my right hand for the mouse, and typically rest my head on my left hand. With a microphone that is close to the face, this can be restrictive. However, when eating, it is beneficial.
In the end, it's up to personal preference, which is why a lot of companies make headphones that allow for the mic to turn 180 degrees, to be usable for both kinds of preferences.
Answer: There does not seem to be a UX answer as far as I can tell. With a little searching, other people seem to believe it is for right hand dominant users where their right hand is always on the mouse and busy so the left hand is available to mess with the headphones. I would also have to agree with this. It seems there isn't a scientific reason for companies doing this though.
Answer: Most people are right-handed, so 90% of headsets are made for r-h people. With right hand you can adjust microphone position more accurate than left.
Answer: Most people have their PCs sitting to the right of their monitor which means the cord has to go across ones body/lap to reach the jack, often rubbing against ones chin along the way. Also, every laptop I've ever owned has had its headphone jack situated to the right, creating the same messy problem with left oriented cords.
Now, of course, someone is going to say 'I always have my PC to the left' or 'my laptop isn't configured like that'. But I believe I am speaking on behalf of the majority of people who live in a right-handed world.
Comment: This should be a comment, not an answer


How do I merge bracketed files together into an HDR image on a Mac?

Question: I'm looking for software in order to put files from bracketing together under Lion and create HDR image.
Comment: You used the HDR tag, so I am going to assume that you want to "put them together" as to create an HDR image.
Comment: exactly. sorry for not being clear
Comment: @Pablo: you can edit the question and title to be more clear.
Comment: @Pablo: Just out of curiosity, is Adobe Photoshop not sufficient? It has a pretty decent HDR merge feature built right in, so if you already have Photoshop (not sure if you do), you already have a tool. Photoshop is also readily available on MacOS.
Comment: @jrista: costly, for non-profit :)
Answer: I am not MAC user, but under Windows I used opensource Luminance HDR and as I see, it is also available for MAC, so you can try it.
Answer: I use Lightroom on a Mac to do most of my usual processing, so I opted for the LR/Enfuse plugin.
It is a "donationware" product, so the price is up to you, and the tool itself works wonderfully, and integrates extremely well into the Lightroom workflow.
Prior to my switch to Lightroom, I used Hugin, which is open source, and also uses enfuse from the panotools project under the covers to do the processing.
Comment: I tried Hugin, but seems it can not work with RAW files, only jpeg?
Comment: Right, not RAW, but it can handle tiff, so you can avoid lossy compression.
Answer: The software I like best currently for HDR merging on the Mac is Nik HDR EFX Pro.  It has a number of different ways you can vary the merge and so far has some of the more realistic tone adjustment I have seen.
http://www.niksoftware.com/hdrefexpro/usa/entry.php
They have an Aperture/Lightroom and Photoshop plugin.
Comment: I tried Nik HDR EFX Pro and the results are pretty exciting. Wondering if I can use it within LR...
Comment: Yes, I think the Aperture plugin is also a LR plugin.  You should be able to download a demote to try out.
Comment: +1 for Nik HDR Efex Pro
works with (as plugin) Adobe Photoshop, Bridge, Lightroom and Apple Aperture (http://www.niksoftware.com/hdrefexpro/en/entry.php?view=de/features.html#multi_app_plugin)
Comment: @el_migu_el The link to Nik Software is no longer valid
Answer: You could look at HDRtist and HDRtist Pro. They are both easy to use programs that on the easier to use side, with adjustment sliders for controlling the HDR effect(s).


Why do we use external pressure to calculate work done by a gas in case of compression instead of using internal pressure?

Question: According to formula work done is (Resisting pressure X dV)? Then in case of compression, isn't the resisting force is internal Pressure? Then why we the formula for compression is same as expansion which is (External Pressure X dV)?
Answer: By Newton's 3rd law, the internal force exerted by the gas on the inside face of the piston is equal to the external force exerted by the inside face of the piston on the gas.  If the expansion or compression is reversible, the internal force can also be determined by the ideal gas law, which applies at essentially thermodynamic equilibrium.  But, if the expansion or compression is irreversible, the internal force cannot be determined using the ideal gas law.  So the only alternative is to use the force manually applied externally (if this is known).


Keep Unhandled layers in QGIS

Question: I saved a .qgs file with home server postgis layers. Had to work remotely, but will lose unhandled layers upon opening the file. I can connect to these through my GeoServer, but I'm going to lose my styles and queries and have to literally rebuild my map from scratch. Is there a way to keep QGIS from dropping these unhandled layers so I can refer to them when rebuilding the map remotely?
Answer: You could set the .qgs file read-only within the file system to prevent dropping the layers.
Or work on a copy of it when working remotely.

Keep Unhandled layers in QGIS


LVM Raid recovery

Question: Ok, I've been beating my head against a wall for 18 hours now with this HDD and cant seem to get anywhere. Heres the backstory. Have had an Iomega EZ-3 Network storage center for about 4 years now. Thing has worked 'alright' most of the time, just slow. So, the other day my dog decides to go to town on it and the chipset just stopped working (on the unit, not the drive). So I remove the drive, plug it into windows and it spins up. Of course, being windows it doesnt like it and just wants to format it. I didnt format it. I tried using some recovery software to get what I can off of it, DiskInternals can see the partitions and disk, but cant do anything with it, their recovery software ran through and found 40k files on it, but without spending 150 I can actually recover any (but the preview window did show my family pics, which is what I want the most). So I tried the Linux route with no luck. I am not a Linux person. So my limited knowledge and experience only drove my frustration further. I booted to testdisk, it saw that there is 2 LVM partitions, 2 RAID, with 1 of each being set to 'D' and two set to 'P'. I tried rewriting the P to the drive, but nothing came back up. I used Gpart, and it sees the drive, sees some small partitions (biggest being 20GB) and the rest unallocated. So I believe the drive is fine, its just the partition is missing. Any help would be greatly appreciated. Got years of family photos on there.
Comment: No formatting, I won't bother to read.
Comment: But you'll bother to comment...
Answer: If you are not concerned about the filenames, photorec could be your answer. This comes with the testdisk suite, and scrapes known filetypes from a raw disk.
iomega likely had a proprietary partitioning scheme on the NAS, meaning it may not be mountable directly from an OS.
Back to testdisk, you don't necessarily need to write a partition table to the disk in order to recover files.
After booting up testdisk with the disk attached, select your disk and partition table. You will likely want to select 'Intel' to start. If you pick the wrong one, no problem. It can scan the drive for partition signatures.
Once you have picked up the partitions, testdisk will tell you a little about them, including partition type and stability. If they are green, they have a readable structure. Hit P to list the files of a particular partition. Deleted files will show up red. You can highlight a folder and press c to copy the files from it. It will ask you where to save it, make sure you have space somewhere. When you are done recovering, keep pressing q to quit.
Sorry if this is redundant information or something you already tried.

LVM Raid recovery


What's the difference between Hibernate and Spring Data JPA

Question: What are the main differences between Hibernate and Spring Data JPA?
When should we not use Hibernate or Spring Data JPA?
Also, when may Spring JDBC template perform better than Hibernate and Spring Data JPA?
Comment: @NeilStockton asking for a comparison between two technologies isn't asking for opinions.
Answer: Hibernate is a JPA implementation, while Spring Data JPA is a JPA data access abstraction. Spring Data JPA cannot work without a JPA provider.
Spring Data offers a solution to the DDD Repository pattern or the legacy GenericDao custom implementations. It can also generate JPA queries on your behalf through method name conventions.
With Spring Data, you may use Hibernate, EclipseLink, or any other JPA provider. A very interesting benefit of using Spring or Java EE is that you can control transaction boundaries declaratively using the @Transactional annotation.
Spring JDBC is much more lightweight, and it's intended for native querying, and if you only intend to use JDBC alone, then you are better off using Spring JDBC to deal with the JDBC verbosity.
Therefore, Hibernate and Spring Data are complementary rather than competitors.
Comment: So does this mean that Spring Data JPA cannot exist of itself? That is, under the hood, it uses one of the JPA provider (like Hibernate, Eclipselink or any other JPA provider)?
Comment: @CuriousMind No, Spring Data does not exist (work) independently. It uses Hibernate implementation by default, you can notice `hibernate-core` as one of child deps in `spring-data-jpa`
Comment: More precisely, so far everything works fine you need only to understand the top level i.e. Spring Data JPA. With the first exception you potentially have to know the lower levels i.e. Hibernate, JDBC **and** the Database.
Comment: I mean if I use only hibernate there is little configuration for hibernate level, If I use spring data jpa, that looks much easier than hibernate. In this sense I asked you that question.
Comment: In general, separation of implementation and API is good design. In this case Spring data implements idea of repository as an API. This API completely based on repository pattern and do not depends on anything else. Hibernate, eclipse Link or other library provide implementation for Spring Data API. From perspective of developers: spring data is required for compilation phase, hibernate is required for runtime phase.
Answer: There are 3 different things we are using here :

JPA : Java persistence api which provide specification for persisting, reading, managing data from your java object to relations in database.
Hibernate: There are various provider which implement jpa. Hibernate is one of them. So we have other provider as well. But if using jpa with spring it allows you to switch to different providers in future.
Spring Data JPA : This is another layer on top of jpa which spring provide to make your life easy.

So lets understand how spring data jpa and spring + hibernate works-

Spring Data JPA:
Let's say you are using spring + hibernate for your application. Now you need to have dao interface and implementation where you will be writing crud operation using SessionFactory of hibernate. Let say you are writing dao class for Employee class, tomorrow in your application you might need to write similiar crud operation for any other entity. So there is lot of boilerplate code we can see here.
Now Spring data jpa allow us to define dao interfaces by extending its repositories(crudrepository, jparepository) so it provide you dao implementation at runtime. You don't need to write dao implementation anymore.Thats how spring data jpa makes your life easy.
Comment: Spring Data JPA used Hibernate implementation by default.. If you see the transitive dependencies of spring-boot-starter-data-jpa, you can see hibernate-core there
Comment: So what is the Spring Data JPA underlying implementation, is it hibernate?because when i use PagingAndSortingRepository, it show Hibernate logs in the console.
Answer: I disagree SpringJPA makes live easy. Yes, it provides some classes and you can make some simple DAO fast, but in fact, it's all you can do.
If you want to do something more than findById() or save, you must go through hell:

no EntityManager access in org.springframework.data.repository classes (this is basic JPA class!)
own transaction management (hibernate transactions disallowed)
huge problems with more than one datasources configuration
no datasource pooling (HikariCP must be in use as third party library)

Why own transaction management is an disadvantage? Since Java 1.8 allows default methods into interfaces, Spring annotation based transactions, simple doesn't work.
Unfortunately, SpringJPA is based on reflections, and sometimes you need to point a method name or entity package into annotations (!). That's why any refactoring makes big crash.
Sadly, @Transactional works for primary DS only :( So, if you have more than one DataSources, remember - transactions works just for primary one :)

What are the main differences between Hibernate and Spring Data JPA?

Hibernate is JPA compatibile, SpringJPA Spring compatibile. Your HibernateJPA DAO can be used with JavaEE or Hibernate Standalone, when SpringJPA can be used within Spring -  SpringBoot for example

When should we not use Hibernate or Spring Data JPA? Also, when may Spring JDBC template perform better than Hibernate / Spring Data JPA?

Use Spring JDBC only when you need to use much Joins or when you need to use Spring having multiple datasource connections. Generally, avoid JPA for Joins.
But my general advice, use fresh solution—Daobab (http://www.daobab.io).
Daobab is my Java and any JPA engine integrator, and I believe it will help much in your tasks :)
Comment: Daobab, really? If you don't like jpql's like of type safety (I don't), JPA has a type safe criteria API... Standard JPA is better than that poor alternative.
Comment: When you use Spring Data JPA there is no reason to need access to `EntityManager`, so this is not a problem. Spring also makes it very easy to manage transactions declaratively; no need to mess with Hibernate transactions yourself. "Huge problems" with more than one data source: this is not very complicated if you know what you are doing. It is also not true that `@Transactional` only works for the primary data source. No datasource pooling: I don't know why you think this, but this is simply not true.
Comment: \@Transactional killed my last project. I'm reading this because I have to learn it over about ORM in spring boot projects to understand how \@Transactional works. Can't say more because I really DONT understand \@Transactional.
Answer: Spring Data is a convenience library on top of JPA that abstracts away many things and brings Spring magic (like it or not) to the persistence store access. It is primarily used for working with relational databases. In short, it allows you to declare interfaces that have methods like findByNameOrderByAge(String name); that will be parsed in runtime and converted into appropriate JPA queries.
Its placement atop of JPA makes its use tempting for:

Rookie developers who don't know SQL or know it badly. This is a
recipe for disaster but they can get away with it if the project is trivial.
Experienced engineers who know what they do and want to spindle up things
fast. This might be a viable strategy (but read further).

From my experience with Spring Data, its magic is too much (this is applicable to Spring in general). I started to use it heavily in one project and eventually hit several corner cases where I couldn't get the library out of my way and ended up with ugly workarounds. Later I read other users' complaints and realized that these issues are typical for Spring Data. For example, check this issue that led to hours of investigation/swearing:
 public TourAccommodationRate createTourAccommodationRate(
        @RequestBody TourAccommodationRate tourAccommodationRate
    ) {
        if (tourAccommodationRate.getId() != null) {
            throw new BadRequestException("id MUST NOT be specified in a body during entry creation");
        }

        // This is an ugly hack required for the Room slim model to work. The problem stems from the fact that
        // when we send a child entity having the many-to-many (M:N) relation to the containing entity, its
        // information is not fetched. As a result, we get NPEs when trying to access all but its Id in the
        // code creating the corresponding slim model. By detaching the entity from the persistence context we
        // force the ORM to re-fetch it from the database instead of taking it from the cache

        tourAccommodationRateRepository.save(tourAccommodationRate);
        entityManager.detach(tourAccommodationRate);
        return tourAccommodationRateRepository.findOne(tourAccommodationRate.getId());
    }

I ended up going lower level and started using JDBI - a nice library with just enough "magic" to save you from the boilerplate. With it, you have complete control over SQL queries and almost never have to fight the library.
Comment: this is a good answer but if you could list down the corner cases and detail it further it will be very helpful. It will become a Great Answer. Thanks
Comment: Well, it's not that easy to recall those given that I haven't worked with Spring Data for quite a while - I will have to go over the old code and will add more comments. However, one of the annoyances straight from my head - in JPA very often objects have to reference each other (e.g. for cascading operations). This causes a Stack Overflow error (due to circular references) when Spring tries to serialize them forcing you into ugly fiddling/workarounds. This issue is tangential to Spring Data though because it's a JPA issue. However, since JPA is underlying Spring Data you get it "for free".
Comment: Edited the post and added one more example.
Comment: For lower level needs, you can just use the JPA provider itself if necessary. The amount of boilerplate database code saved with Spring Data JPA is wonderful and for higher level database operations you don't need the verbosity of a Hibernate CriteriaQuery. Why would you write all this extra code if it's not needed?
Answer: If you prefer simplicity and more control on SQL queries then I would suggest going with Spring Data/ Spring JDBC.
Its good amount of learning curve in JPA and sometimes difficult to debug issues.
On the other hand, while you have full control over SQL, it becomes much easier to optimize query and improve performance. You can easily share your SQL with DBA or someone who has a better understanding of Database.
Answer: Hibernate is implementation of "JPA" which is a specification for Java objects in Database.
I would recommend to use w.r.t JPA as you can switch between different ORMS.
When you use JDBC then you need to use SQL Queries, so if you are proficient in SQL then go for JDBC.
Comment: I'm guilty of using WRT sometimes, myself.  Your use here doesn't make sense.
Comment: with respect to
Comment: What is w.r.t.?