Tales from the jar side: Spring's queryForStream method, A Marlborough, CT election and demographics, and Good Tweets
Dad joke: I went to a haunted bread-and-breakfast in France. I had to leave. That place was giving me the crêpes.
Welcome, fellow jarheads, to Tales from the jar side, the Kousen IT newsletter, for the week of May 1 - May 8, 2022. This week I taught another week-long Java and Spring course for a private client composed of Ruby on Rails and C# developers.
Regular readers of this newsletter are affectionately known as jarheads, and are far more intelligent, sophisticated, and attractive than the average newsletter reader. If you wish to become a jarhead, please subscribe using this button:
As a reminder, this message is truncated in email, click on the title at the top to open it in a browser tab formatted properly for both the web and mobile.
I started the last two newsletters with a side comment about Stephen Curry, the Greatest Free Throw Shooter in NBA History, missing multiple free throws in a playoff game. I’m happy to report that last night, in Golden State’s blowout victory over the Memphis Grizzlies, Steph made 14 out of 14 free throws. In a chaotic world, it’s nice to know there are some things you can rely on.
Here is this week’s technical content. As usual, feel free to skip it if you’re not into that sort of thing.
Spring’s queryForStream Method
During my regular Spring course, I talk about the three major ways to access relational data from Spring:
Send already-worked-out SQL using the
jdbcTemplate
,Work with Hibernate by injecting the JPA
EntityManager
using the@PersistenceContext
annotation, andSimply extend one of the Spring Data interfaces, like
CrudRepository
.
I told my students how I always struggle to implement the findById
method using the jdbcTemplate
. The method signature is:
Optional<Officer> findById(Integer id)
If the id
exists in the database, I want to return that Officer
, wrapped in an Optional
. If the id
does not exist, I want to return an empty Optional
. My original hope was that I could call the queryForObject
method, and wrap the result in an Optional
:
jdbcTemplate.queryForObject(
"SELECT * FROM officers WHERE id=?", officerMapper, id));
The problem is that when you call queryForObject
with an id
that is not in the database, the method doesn’t return null
, it throws an exception (specifically, an IncorrectResultSizeDataAccessException
).
That means I have two choices. I can either catch that exception and return the empty Optional
from the catch
block:
This works, but makes the exception handling part of the normal flow of execution, or I can check first to see if the id
exists, and then make the call:
This also works, but at the cost of an additional database query. I was describing the dilemma and trade-offs with the students, when one person (Hi Ian!) suggested an alternative: use queryForStream
combined with findFirst
or findAny
. After checking the documentation, I realized I need to wrap that in a try-with-resources block, but here’s the result:
Perfect, and it never occurred to me. I knew Spring had added the queryForStream
method, but I never realized I had the ideal opportunity to use it. I’ve been using this example for about five years or so, but now, at last, I think I know how to handle it properly. Good thing I went to class last week.
I Voted
I voted last Tuesday, May 3.
My town of Marlborough, CT, decided last year to have an election to add two more members to the Board of Selectmen, bringing the total to five.
(Yes, Selectmen, even though both winners and many previous members are women. It’s New England, so the term goes back a few hundred years.)
I wasn’t going to bother this time. I taught a five-day, full-day Java and Spring class this week, and while it went very well and I enjoyed it, it was tiring. I remembered the election around 7:30pm and mentioned it to my wife, who immediately said we were going. The polls closed at 8pm, but we made it pretty comfortably.
The unofficial election results showed up in a private Facebook group for our town a couple of days later — Marlborough is too small and this election was apparently not important enough to make the Hartford Courant, our local major paper. You can see the full results in the Rivereast News Bulletin (warning: link is a pdf) that came out on Friday.
To my considerable surprise, the Democrats won. As I like to say, my town is so small we only have two (count them, two) stoplights. We share a high school with the towns of Andover and Hebron, which is why the high school is called RHAM, for Regional Hebron, Andover, and Marlborough. Of the three, we’re the relatively upscale community. Between the small town vibe and the money, we have a lot of Republicans. It’s rare for Democrats to win anything here.
Looking further at the census data, I see that my goodness my town is lily white (95.5%), which is probably another reason for all those Republicans. Wow. Welcome to rural Connecticut, I guess.
Rural vs Urban
Speaking of rural, this week an article appeared in USA Today announcing that the Census Bureau was redefining what qualifies as rural as opposed to urban. The new criteria counts housing units instead of people. As the article says:
A place had to have at least 2,500 people to be urban under old criteria that lasted more than a century. Now, it will need at least 2,000 housing units, the equivalent of about 5,000 people.
We’re pretty small and heavily wooded, but despite its size the town doesn’t feel all that rural, so that made me curious. Official results won’t be out until late in the year, but I was able to find the census information for my town, as of April, 2020. The numbers show that we have 6133 people in Marlborough, and while the number of housing units is unavailable, the number of households is 2289. I expect we’ll still be considered urban (or, more accurately, suburban, though that’s not an option in the table) rather than rural when all is said and done.
Other measures: we have a Dunkin’ Donuts, but not a Starbucks. We have a Subway, but not a McDonald’s or any other fast food restaurant. A couple of years ago we finally got our own big grocery store (a Big Y, rather than a Stop and Shop, for some reason). Our local Bank of America branch closed, which was probably a pandemic casualty, plus the fact that so many people use the online systems now, but we still have a Liberty Bank, whatever that is. We still have our own tiny post office, too.
Tweets And Such
GitHub Themes
I didn’t know you could do this:
After hitting Command/Control-K, type “> theme”. Cute.
TL;DR
This is a great Tom Gauld cartoon:
If you like, feel free to send me one of these as a reply to this newsletter.
Happy Mother’s Day
Your Mission…
… should you choose to accept it, is to offend as many science fiction communities as possible in a single image. Try this one on for size:
80’s Reference of the Week
This tweet cracked me up:
I can’t believe I have to say this, but it’s from Pink Floyd’s The Wall (1979), which I didn’t even like but freely acknowledge its historical importance.
Just Another Manic Sunday
Finally, I have a confession to make. I try not to make mistakes, but they do happen, and I feel obligated to admit it when I make one. Last Sunday in this newsletter, when I included a tweet by Susanna Hoffs (former lead singer of The Bangles) where she admitted that Saturday is her actual fun day, I claimed that their best song was Manic Monday. I also included the associated video. Sadly, I realized almost immediately that I was wrong. Clearly the best Bangles song is Eternal Flame:
I apologize for the error and will try to do better in the future.
As a reminder, you can see all my upcoming training courses on the O’Reilly Learning Platform here and all the upcoming NFJS Virtual Workshops here.
Last week:
Private class on Java and Spring for Ruby on Rails and C# developers.
This week:
Introduction to the Gradle Build Tool, online for Gradle, Inc
Central Iowa Software Symposium, the NFJS event in Des Moines, IA, May 13 - 14. I’m scheduled to do what we call a Royal Venkat, which means I’m giving 5 talks on Friday, 5 more on Saturday, and doing the keynote Friday evening. I plan to sleep a lot on the flights home on Sunday. After sending out my newsletter, of course.
SR; PW