我的executeUpdate()工作正常,但没有提交

时间:2015-08-11 20:27:38

标签: java postgresql jboss datasource

我正在尝试在postgresql数据库中插入数据,但是,当我执行executeUpdate()方法时,它不会在我的数据库中插入任何数据,而且我无法看到我在哪里做错了...

Ps:我的数据库是autocommit:on;

我在这里使用Jboss 7.1.1我的dataSource配置:

<subsystem xmlns="urn:jboss:domain:datasources:1.0">
            <datasources>
                <datasource jta="true" jndi-name="java:jboss/datasources/PostgresqlDS" pool-name="PostgreDS" enabled="true" use-java-context="true" use-ccm="true">
                    <connection-url>jdbc:postgresql://dataBaseAddress/dataBaseName</connection-url>
                    <driver>org.postgresql</driver>
                    <pool>
                        <min-pool-size>2</min-pool-size>
                        <max-pool-size>20</max-pool-size>
                    </pool>
                    <security>
                        <user-name>user</user-name>
                        <password>password</password>
                    </security>
                </datasource>
                <drivers>
                    <driver name="org.postgresql" module="org.postgresql">
                        <xa-datasource-class>org.postgresql.xa.PGXADataSource</xa-datasource-class>
                    </driver>
                    <driver name="h2" module="com.h2database.h2">
                        <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
                    </driver>
                </drivers>
            </datasources>
        </subsystem>

这是我的连接类:

public Connection getConnection() throws TopLevelException, SQLException {
        Connection conn = null;
        try {
              Context ctx = new InitialContext();
              TransactionBean tal = (TransactionBean) ctx.lookup("java:global/snrng-ear/snrng-ejb-lgc/TransactionBean!br.com.compplied.snrng.ejb.TransactionBean");
              conn = tal.getConnection();
          } catch (NamingException e) {
              throw new TopLevelException(e);
          }
          return conn;
    }

这是执行我的插入

的方法
public int inserirHistorico(RetornoHistoricoObject retorno) throws TopLevelException {
        int update = 0;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Connection con = null;
        String sql = "INSERT INTO table ( column1, column2, column3, column4, column5, column6) values (?, ?, ?, ?, ?, localtimestamp)";
        try {
            con = getConnection();
            ps = con.prepareStatement(sql);
            rs = ps.getResultSet();
            ps.setString(1, retorno.getNome_arquivo());
            ps.setString(2, retorno.getNumero_autenticacao().trim());
            ps.setString(3, retorno.getNosso_numero());
            ps.setDate(4, retorno.getData_pagamento());
            ps.setDouble(5, retorno.getValor());
            update = ps.executeUpdate();  

        } catch (SQLException e) {
            throw new TopLevelException(e);
        } catch (Exception e) {
            throw new TopLevelException(e);
        } finally {
            try {
                close(rs, ps, con);
            } catch (SQLException e) {
                throw new TopLevelException(e);
            }
        }
        return update;
    }   

当我执行ps.executeUpdate()方法时,向我返回一个插入了新id的成功消息,但是,当我在我的表上查找此id时,没有插入任何内容。我已经检查了我的数据库参数,连接等等,但它仍然无法正常工作......任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

为了解决问题,请尝试:

  1. 在DB中执行手动插入以检查自动提交设置?如果工作 - Java中的问题。如果不起作用 - DB中的问题

  2. 创建一个简单的类来执行插入并检查它是否有效。它工作,应用程序设置的问题。如果不起作用 - FU ##使用Java / Driver!

  3. 如果1和2有效,请尝试使用交易:

    Transaction t =  beginTransaction(); // session???
    //your code here
    tx.commit;