j2ee注入连接而非事务管理

时间:2017-03-09 17:56:08

标签: java java-ee jdbc cdi wildfly-10

我正在使用CDI在我的DAO类中注入Connection。

连接生成器是这样的:

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方法中调用dao方法。

连接被注入并在当前线程的范围内正确关闭,但在执行任何查询之后,它立即在数据库上提交。

我使用的数据源是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>

我正在使用postgresql和WildFly 10

有人可以解释一下为什么会这样吗?

抱歉我的英文不好

1 个答案:

答案 0 :(得分:0)

由于您可以访问Connection实例,是否可以尝试将自动提交设置为false?

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

根据doc,默认设置为true。

顺便说一句,我认为你不应该在CDI生产者中生成连接,而是在try-with-resources中创建它:

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