I'm using entity manager to create a table with native query,and insert rows to another table with em.persist(). when the native query which is create table failed,I rolled back,but the em.persist() is always successful.Does anyone know the answer??
I wrote the wrong sql in purpose to make it into catch.
try {
tx.begin();
table1 t1 = new table1();
t1.setC1(c1);
t1.setC2(c2);
EntityManager em = emf.createEntityManager();
em.persist(t1);
Query query1= em.createNativeQuery("drop table if exists table2").executeUpdate();
Query query2= em.createNativeQuery("xxxxxxxxxxxxxxxx").executeUpdate();
tx.commit();
rt = true;
} catch (Exception e) {
try {
tx.rollback();
} catch (Exception e1) {
PUB.log(e, PUB.getLineInfo());
}
}
here is the persistence.xml and i'm using Mysql5.6:
@Stateless
@TransactionManagement(TransactionManagementType.BEAN)
public class MainSessionBean implements MainSessionBeanRemote {
@PersistenceUnit(unitName = "CC2016JPA2")
static EntityManagerFactory emf;
@Resource
UserTransaction tx;