Criando savepoints para conexões de banco de dados usando JDBC
Aprenda a criar savepoints e tenha como recuperar seus dados em casos de falha nas transações do banco de dados.
Criando savepoints para conexões de banco de dados usando JDBC
Criar savepoints é um comportamento bastante comum quando existem muitas transações no banco de dados. Com isso, em caso de erro ou execessão, é possível fazer um rollback dessas transações com a ajuda dos savepoints.
<B>import </B></FONT>java.sql.*;</FONT><BR></FONT><BR><B>public class </B></FONT>testSavePoint </FONT><BR>{</FONT><BR> </FONT><B>public static </B></FONT><B>void </B></FONT>main</FONT>(</FONT>String</FONT>[] </FONT>args</FONT>) </FONT><BR> </FONT>{</FONT><BR> </FONT>DB db = </FONT><B>new </B></FONT>DB</FONT>()</FONT>;</FONT><BR> </FONT>Connection conn=db.dbConnect</FONT>(</FONT><BR> </FONT>"jdbc:jtds:sqlserver://localhost:1433/tempdb"</FONT>,</FONT>"sa"</FONT>,</FONT>""</FONT>)</FONT>;</FONT><BR> </FONT>db.performSavePoint</FONT>(</FONT>conn</FONT>)</FONT>;</FONT><BR> </FONT>}</FONT><BR>}</FONT><BR></FONT><BR><B>class </B></FONT>DB</FONT><BR>{</FONT><BR> </FONT><B>public </B></FONT>DB</FONT>() {}</FONT><BR></FONT><BR> </FONT><B>public </B></FONT>Connection dbConnect</FONT>(</FONT>String db_connect_string, </FONT><BR> </FONT>String db_userid, String db_password</FONT>)</FONT><BR> </FONT>{</FONT><BR> </FONT><B>try</B></FONT><BR> </FONT>{</FONT><BR> </FONT>Class.forName</FONT>(</FONT>"net.sourceforge.jtds.jdbc.Driver"</FONT>)</FONT>;</FONT><BR> </FONT>Connection conn = DriverManager.getConnection</FONT>(</FONT><BR> </FONT>db_connect_string, db_userid, db_password</FONT>)</FONT>;</FONT><BR> </FONT>System.out.println</FONT>(</FONT>"connected"</FONT>)</FONT>;</FONT><BR> </FONT><B>return </B></FONT>conn;</FONT><BR> </FONT><BR> </FONT>}</FONT><BR> </FONT><B>catch </B></FONT>(</FONT>Exception e</FONT>)</FONT><BR> </FONT>{</FONT><BR> </FONT>e.printStackTrace</FONT>()</FONT>;</FONT><BR> </FONT><B>return null</B></FONT>;</FONT><BR> </FONT>}</FONT><BR> </FONT>}</FONT><BR></FONT><BR> </FONT><B>public </B></FONT><B>void </B></FONT>performSavePoint</FONT>(</FONT>Connection conn</FONT>)</FONT><BR> </FONT>{</FONT><BR> </FONT><B>try</B></FONT><BR> </FONT>{</FONT><BR> </FONT>conn.setAutoCommit</FONT>(</FONT><B>false</B></FONT>)</FONT>;</FONT><BR></FONT><BR> </FONT>String query = </FONT><BR> </FONT>"select id,name,category,age from customer where age > ?"</FONT>;</FONT><BR> </FONT>String update = </FONT>"update customer set age = ? where id = ?"</FONT>;</FONT><BR></FONT><BR> </FONT>PreparedStatement pstmt1 = conn.prepareStatement</FONT>(</FONT>query</FONT>)</FONT>;</FONT><BR> </FONT>PreparedStatement pstmt2 = conn.prepareStatement</FONT>(</FONT>update</FONT>)</FONT>;</FONT><BR></FONT><BR> </FONT>pstmt1.setInt</FONT>(</FONT>1</FONT>, </FONT>25</FONT>)</FONT>;</FONT><BR> </FONT>ResultSet rs = pstmt1.executeQuery</FONT>()</FONT>;</FONT><BR></FONT><BR> </FONT>Savepoint save1 = conn.setSavepoint</FONT>()</FONT>;</FONT><BR> </FONT><B>while </B></FONT>(</FONT>rs.next</FONT>()) </FONT><BR> </FONT>{</FONT><BR> </FONT><B>int </B></FONT>id = rs.getInt</FONT>(</FONT>"id"</FONT>)</FONT>;</FONT><BR> </FONT>String name = rs.getString</FONT>(</FONT>"name"</FONT>)</FONT>;</FONT><BR> </FONT>String category = rs.getString</FONT>(</FONT>"category"</FONT>)</FONT>;</FONT><BR> </FONT><B>int </B></FONT>age = rs.getInt</FONT>(</FONT>"age"</FONT>)</FONT>;</FONT><BR> </FONT><B>int </B></FONT>newage = age + </FONT>5</FONT>;</FONT><BR> </FONT>pstmt2.setInt</FONT>(</FONT>1</FONT>, newage</FONT>)</FONT>;</FONT><BR> </FONT>pstmt2.setInt</FONT>(</FONT>2</FONT>, id</FONT>)</FONT>;</FONT><BR> </FONT>pstmt2.executeUpdate</FONT>()</FONT>;</FONT><BR> </FONT><B>if </B></FONT>(</FONT>newage > </FONT>39</FONT>) </FONT><BR> </FONT>{</FONT><BR> </FONT>conn.rollback</FONT>(</FONT>save1</FONT>)</FONT>;</FONT><BR> </FONT>}</FONT><BR></FONT><BR> </FONT>}</FONT><BR> </FONT>conn.commit</FONT>()</FONT>;</FONT><BR> </FONT>}</FONT><BR> </FONT><B>catch </B></FONT>(</FONT>Exception e</FONT>)</FONT><BR> </FONT>{</FONT><BR> </FONT>e.printStackTrace</FONT>()</FONT>;</FONT><BR> </FONT>}</FONT><BR> </FONT>}</FONT><BR>}</FONT>;</FONT> |
Artigos relacionados
-
Artigo
-
Artigo
-
Artigo
-
Artigo
-
Artigo