2017-03-09 47 views
0

我使用CDI在我的DAO类中注入连接。j2ee注入连接不是事务管理

连接制片人是这样的:

public class ConnectionManager { 
    private static final Logger LOGGER = Logger.getLogger(ConnectionManager.class.getName()); 

    @Resource(mappedName = "java:/PostgresXADS") 
    private DataSource flamingoDs; 

    @Named("flamingoConnection") 
    @Produces 
    @RequestScoped 
    public Connection createFlamingoConnection() { 
     LOGGER.info("createFlamingoConnection called"); 
     try { 
      return flamingoDs.getConnection(); 
     } catch (SQLException e) { 
      throw new RuntimeException(e); 
     } 
    } 
    public void closeConnection(@Disposes Connection c) { 
     LOGGER.info("closeConnection called"); 
     try { 
      c.close(); 
     } catch (SQLException e) { 
      throw new RuntimeException(e); 
     } 
    } 
} 
在我的例如DAO类

然后,我有:

public class GraphDao { 

@Inject 
@Named("flamingoConnection") 
Connection con; 

public String createObjNode(String tipoCod, String nome, Object flmNodeData) 
     throws JsonProcessingException, SQLException, IllegalArgumentException { 
    if (tipoCod == null || flmNodeData == null || nome == null) { 
     throw new IllegalArgumentException("Empty parameters"); 
    } 
    LOGGER.info("createObjNode start"); 
    String generatedUuid; 
    String sql = "INSERT INTO graphdb.nodo_oggetto (tipo_cod, nome, dati) VALUES(?, ? ,?) RETURNING uuid"; 
    PGobject jsonObject = getJsonPgObj(flmNodeData); 
    try (PreparedStatement stmt = this.con.prepareStatement(sql);) { 
     stmt.setObject(1, tipoCod); 
     stmt.setString(2, nome); 
     stmt.setObject(3, jsonObject); 
     try (ResultSet rs = stmt.executeQuery()) { 
      rs.next(); 
      generatedUuid = rs.getString(1); 
     } 
    } 
    LOGGER.info("createObjNode end"); 
    return generatedUuid; 
} 

我打电话EJB方法里面的道法的容器管理事务。

连接在当前线程的作用域中被注入并正确关闭,但在执行任何查询后立即在数据库上提交。

我使用的数据源类型XA它的定义是:

<xa-datasource jndi-name="java:/PostgresXADS" pool-name="PostgresXADS" enabled="true" use-ccm="true"> 
    <xa-datasource-property name="url"> 
     jdbc:postgresql://localhost:5433/infostud?ApplicationName=NewSegr 
    </xa-datasource-property> 
    <driver>postgres</driver> 
    <xa-pool> 
     <prefill>true</prefill> 
     <is-same-rm-override>false</is-same-rm-override> 
     <no-tx-separate-pools>true</no-tx-separate-pools> 
     <wrap-xa-resource>true</wrap-xa-resource> 
    </xa-pool> 
    <security> 
     <user-name>XXX</user-name> 
     <password>XXX</password> 
    </security> 
    <validation> 
     <valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLValidConnectionChecker"/> 
     <background-validation>true</background-validation> 
     <exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLExceptionSorter"/> 
    </validation> 
</xa-datasource> 
<drivers> 
    <driver name="postgres" module="org.postgresql"> 
     <xa-datasource-class>org.postgresql.xa.PGXADataSource</xa-datasource-class> 
     <datasource-class>org.postgresql.ds.PGPoolingDataSource</datasource-class> 
</driver> 

我unsing PostgreSQL和WildFly 10

是否有人可以解释我为什么发生?

对不起我的英文不好

+0

你能提供,你把这个'createObjNode为例()'它立即提交到数据库? – ytg

回答

0

既然你可以访问连接实例,你可以尝试设置自动提交到假的?

Connection conn = flamingoDs.getConnection(); 
conn.setAutoCommit(false); 
return conn; 

根据doc,默认设置为true。

顺便说一句,我想你不应该产生一个CDI生产的连接,而是在try-与资源创建:

try (Connection connection = dataSource.getConnection(); 
      PreparedStatement stmt = connection.prepareStatement(sql))