In men, around 70% of UTIs are caused by outflow obstructions (peeing slowly), resulting in incomplete voiding (not emptying your bladder before you stop peeing).
That may be what triggered your first one, since prostate cancer can cause outflow obstruction.
Is your flow rate slow now? It would not be surprising if you were at a higher risk while the surgery was still healing, even if flow rate is normal, so it might be that the two UTIs are not related.
You might try double voiding, to check that you are fully emptying your bladder. You do this by waiting 30 seconds after peeing, and then try peeing again. If you get a reasonable amount out (and not just a few drips produced by your kidneys in the 30 seconds), then you are still incomplete voiding, which raises your chances of a UTI.