The Wikipedia article traces a likely origin to songs by American immigrants about the difficulty of getting people to recognize and properly pronounce their native names.
While the origins of the song are obscure, some evidence places its roots with vaudeville and theatre acts of the late 19th century and early 20th century popular in immigrant communities. Some vaudeville acts during the era, such as the work of Joe Weber and Lew Fields, often gave voice to shared frustrations of German-American immigrants and heavily leaned on malapropisms and difficulties with the English language as a vehicle for their humor. "John Jacob Jingleheimer Schmidt" shares many characteristics with "My Name Is Jan Jansen", a song that can trace its origin to Swedish vaudeville in the late 19th century.
So it's not so much a matter of "we have the same name" so much as their shared experience. While not directly referenced in the Wikipedia article, I suspect that there's a factor of frequent repetition of the unfamiliar name from an experiencing of the exotic by the speaker, a reaffirmation because they're having trouble remembering it properly, or because they're worried that if they don't include the whole name, they'll quote the wrong part. E.g., someone might repeatedly reference a new Japanese coworker as "Takahashi Rumiko" because they find the whole name poetic sounding, because they want to reinforce in their head that it is "Rumiko" and not some similar name in their head, and because they at one point accidentally referred to her as "Takahashi" and had to be reminded that that is her family name.