Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Java
tricky jdbc issue
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Qaran, post: 610696"] 1. The following code is a complete application, which accesses a **shared** SQL database. This code seemed ok in test but causes some unrelated updates of salary to be occasionally lost in production. Why could this happen? What options are there to resolve this problem? Hint: Focus on coreLogic. import java.math.BigDecimal; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class Operation { private Connection c; private void coreLogic(long empId) throws SQLException { BigDecimal salary = selectSalary(empId); salary = indexSalary(salary); updateSalary(empId, salary); } public void execute(Connection aConn, long empId) throws SQLException { c = aConn; c.setAutoCommit(false); try { coreLogic(empId); c.commit(); } catch (SQLException e) { c.rollback(); throw e; } } private BigDecimal selectSalary(long empId) throws SQLException { PreparedStatement ps = c.prepareStatement( "SELECT salary FROM emp WHERE emp_id = ?"); ResultSet rs = null; try { ps.setLong(1, empId); rs = ps.executeQuery(); rs.next(); return rs.getBigDecimal("salary"); } finally { if (rs != null) { rs.close(); } ps.close(); } } private BigDecimal indexSalary(BigDecimal salary) { return salary.multiply(new BigDecimal("1.1")); } private void updateSalary(long empId, BigDecimal salary) throws SQLException { PreparedStatement ps = c.prepareStatement( "UPDATE emp SET salary = ? WHERE emp_id = ?"); try { ps.setBigDecimal(1, salary); ps.setLong(2, empId); ps.executeUpdate(); } finally { ps.close(); } } public static void main(String args[]) throws Exception { Class.forName(args[0]); Connection c = DriverManager.getConnection(args[1], args[2], args[3]); Operation op = new Operation(); op.execute(c, Long.parseLong(args[4])); } } Sample DDL is for emp is: CREATE TABLE emp ( emp_id NUMERIC PRIMARY KEY, salary NUMERIC NOT NULL); [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Java
tricky jdbc issue
Top