Genesis 38 describes the awful consequences of Judah leaving his brothers and pursing his own desires. Two of Judah’s children were put to death by God for their wickedness, and Judah’s daughter-in-law acts as a prostitute in order to blackmail Judah into allowing her to marry Judah’s final son. A very graphic story, but a reminder of what happens when we leave God behind.
By contrast, David’s “soul thirsts for God”, even when he was mourning and suffering at the hands of his enemies. God sustains and restores David when he is sick, and even though David repeatedly refers to his soul being cast down, his hope remains sure, “For you are the God in whom I take refuge… Hope in God, for I shall again praise him, my salvation and my God”.