Skip to content

Commit

Permalink
add Connection#setAutoCommit(false)
Browse files Browse the repository at this point in the history
  • Loading branch information
MasatoshiTada committed Nov 4, 2015
1 parent b7c9250 commit 276ae32
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/main/java/com/example/jdbc/JdbcCdiRequiredTestDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import javax.annotation.Resource;
import javax.enterprise.context.Dependent;
import javax.enterprise.context.RequestScoped;
import javax.sql.DataSource;
import javax.transaction.Transactional;
import java.io.IOException;
Expand All @@ -16,7 +17,8 @@
*
* @author tada
*/
@Dependent
//@Dependent
@RequestScoped
public class JdbcCdiRequiredTestDao implements Serializable {

@Resource(lookup = "jdbc/sandbox")
Expand Down Expand Up @@ -53,9 +55,15 @@ public int deleteAll() throws Exception {
}
}

private Connection getNonAutoCommitConnection() throws Exception {
Connection con = dataSource.getConnection();
con.setAutoCommit(false);
return con;
}

private int insert(TestEntity testEntity) throws Exception {
String sql = "INSERT INTO test_entity(id, thrown, rollbackon, dontrollbackon, expected) VALUES(?, ?, ?, ?, ?)";
try (Connection con = dataSource.getConnection();
try (Connection con = this.getNonAutoCommitConnection();
PreparedStatement ps = con.prepareStatement(sql)) {
ps.setInt(1, testEntity.getId());
ps.setString(2, testEntity.getThrown());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,15 @@ public int deleteAll() throws Exception {
}
}

private Connection getNonAutoCommitConnection() throws Exception {
Connection con = dataSource.getConnection();
con.setAutoCommit(false);
return con;
}

private int insert(TestEntity testEntity) throws Exception {
String sql = "INSERT INTO test_entity(id, thrown, rollbackon, dontrollbackon, expected) VALUES(?, ?, ?, ?, ?)";
try (Connection con = dataSource.getConnection();
try (Connection con = this.getNonAutoCommitConnection();
PreparedStatement ps = con.prepareStatement(sql)) {
ps.setInt(1, testEntity.getId());
ps.setString(2, testEntity.getThrown());
Expand Down

0 comments on commit 276ae32

Please # to comment.