Announcement Announcement Module
Collapse
No announcement yet.
Does Spring Framework support Select for Update Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Does Spring Framework support Select for Update

    Can someone show me how the 'Select for Update' work in Spring Framework?

    Thanks.

  • #2
    I think this is something that you do in your persistence framework. For instance, Hibernate allows for optimistic locking that actually defaults to use versioning which is better performing than SELECT FOR UPDATE.

    Lou

    Comment


    • #3
      If using Spring JDBC you can simply use the "FOR UPDATE" syntax in your SQL queries. If you use Hibernate or another ORM tool with Spring, you should refer to the documentation on using pessimistic locking with that tool: Spring will leave locking to the persistence layer.

      Spring JDBC does understand and correctly translate SQL exceptions resulting from SELECT FOR UPDATE NOWAIT btw.

      Comment


      • #4
        There is also UpdatableSqlQuery that operates on an updatable ResultSet. It lets you map/update row values in an updateRow callback method. It is similar to the MappingSqlQuery class except that it lets you update the rows as they pass through.
        Code:
        	private class MyUpdate extends UpdatableSqlQuery {
        		private final static String sql = "select id, price from booking where id > ?";
        		
        		public MyUpdate(DataSource ds) {
        			super(ds, sql);
        			declareParameter(new SqlParameter("id", Types.INTEGER));
        			compile();
        		}
        
        		protected Object updateRow(ResultSet rs, int rowNum, Map context) throws SQLException {
        			BigDecimal price = rs.getBigDecimal(2);
        			price = price.multiply(new BigDecimal(1.1));
        			rs.updateBigDecimal(2, price);
        			return price;
        		}
        		
        	}

        Comment

        Working...
        X