View Javadoc
1   package net.avcompris.status.dao.impl;
2   
3   import static com.google.common.base.Preconditions.checkNotNull;
4   
5   import java.io.IOException;
6   import java.sql.Connection;
7   import java.sql.PreparedStatement;
8   import java.sql.ResultSet;
9   import java.sql.SQLException;
10  import java.sql.SQLIntegrityConstraintViolationException;
11  
12  import javax.sql.DataSource;
13  
14  import org.joda.time.DateTime;
15  import org.springframework.beans.factory.annotation.Autowired;
16  import org.springframework.beans.factory.annotation.Value;
17  import org.springframework.stereotype.Component;
18  
19  import net.avcompris.commons3.dao.CorrelationDao;
20  import net.avcompris.commons3.dao.exception.DuplicateEntityException;
21  import net.avcompris.commons3.dao.impl.AbstractDaoInRDS;
22  import net.avcompris.commons3.utils.Clock;
23  
24  @Component
25  public final class CorrelationDaoInRDS extends AbstractDaoInRDS implements CorrelationDao {
26  
27  	private final boolean debug;
28  
29  	@Autowired
30  	public CorrelationDaoInRDS( //
31  			@Value("#{rds.dataSource}") final DataSource dataSource, //
32  			@Value("#{rds.tableNames.correlations}") final String tableName, //
33  			final Clock clock) {
34  
35  		super(dataSource, tableName, clock);
36  
37  		debug = System.getProperty("debug") != null;
38  	}
39  
40  	@Override
41  	public boolean isCorrelationIdValid(final String correlationId) throws SQLException {
42  
43  		checkNotNull(correlationId, "correlationId");
44  
45  		try (Connection cxn = getConnection()) {
46  
47  			try (PreparedStatement pstmt = cxn.prepareStatement("SELECT" //
48  					+ " 1" //
49  					+ " FROM " + tableName //
50  					+ " WHERE correlation_id = ?" //
51  			)) {
52  
53  				setString(pstmt, 1, correlationId);
54  
55  				try (ResultSet rs = pstmt.executeQuery()) {
56  
57  					if (rs.next()) {
58  
59  						return true;
60  					}
61  				}
62  			}
63  		}
64  
65  		return false;
66  	}
67  
68  	@Override
69  	public void addCorrelationId(final String correlationId)
70  			throws SQLException, IOException, DuplicateEntityException {
71  
72  		checkNotNull(correlationId, "correlationId");
73  
74  		final DateTime now = clock.now();
75  
76  		try (Connection cxn = getConnection()) {
77  
78  			if (debug) {
79  				final long elapsedMs = System.currentTimeMillis() - now.getMillis();
80  				System.out.println(CorrelationDaoInRDS.class.getSimpleName() + ".addCorrelationId(): " + correlationId
81  						+ ": getConnection(), elapsedMs: " + elapsedMs);
82  			}
83  
84  			try (PreparedStatement pstmt = cxn.prepareStatement("INSERT INTO " + tableName //
85  					+ " (correlation_id, created_at)" //
86  					+ " VALUES (?, ?)" //
87  			)) {
88  
89  				setString(pstmt, 1, correlationId);
90  				setDateTime(pstmt, 2, now);
91  
92  				try {
93  
94  					pstmt.executeUpdate();
95  
96  				} catch (final SQLIntegrityConstraintViolationException e) {
97  
98  					throw new DuplicateEntityException("Duplicate correlationId: " + correlationId, e);
99  				}
100 			}
101 		}
102 
103 		if (debug) {
104 			final long elapsedMs = System.currentTimeMillis() - now.getMillis();
105 			System.out.println(CorrelationDaoInRDS.class.getSimpleName() + ".addCorrelationId(): " + correlationId
106 					+ ": total: elapsedMs: " + elapsedMs);
107 		}
108 	}
109 
110 	@Override
111 	public void purgeOlderThanSec(final int seconds) throws SQLException, IOException {
112 
113 		final DateTime olderThan = clock.now().minusSeconds(seconds);
114 
115 		try (Connection cxn = getConnection()) {
116 
117 			try (PreparedStatement pstmt = cxn.prepareStatement("DELETE FROM " + tableName //
118 					+ " WHERE created_at < ?" //
119 			)) {
120 
121 				setDateTime(pstmt, 1, olderThan);
122 
123 				pstmt.executeUpdate();
124 			}
125 		}
126 	}
127 }