Well, didn't discover how to do it with container-managed transactions, but did it with bean-managed transactions. Injected a UserTransaction resource, and wrapped each persist between a begin/commit pair:
@Stateless
@TransactionManagement(TransactionManagementType.BEAN)
public class MyClass
{
@Resource private UserTransaction utx;
public void doStuff()
{
EntityManager mysql = ...;
EntityManager postgres = ...;
MyEntity e = new MyEntity();
e.setStuff();
try {
utx.begin();
mysql.persist(e);
utx.commit();
utx.begin();
postgres.persist(e);
utx.end();
} catch (...) {
...
}
}
}
I've never tried using bean-managed transactions before, and even though this isn't for production use and isn't especially elegant, if there's something fundamentally wrong with this, I'd appreciate somebody pointing out the proper way.