java 代码
- /**
- * The SQL statement to insert the entity details.
- */
- private static final String INSERT_DETAILS = "INSERT INTO message (message_id, from_handle, project_id, "
- + "project_name, timestamp, subject, message) VALUES (?, ?, ?, ?, ?, ?, ?)" ;
- /**
- * Creates a new database connection using the configured database connection factory and connection name.
- *
- * @return a new database connection created using the configured database connection factory and
- * connection name
- * @throws MessagePersistenceException if an error occurs while creating the connection
- */
- private Connection getConnection() {
- logEntry("getConnection()" );
- Connection conn = null ;
- try {
- this .logExternalCall( "calling connectionFactory.createConnection({0})" , connectionName);
- conn = this .connectionFactory.createConnection(connectionName);
- this .logExternalCall( "returned from connectionFactory.createConnection({0})" , connectionName);
- } catch (DBConnectionException ex) {
- this .throwPersistenceException(ex, "error creating connection: " );
- } finally {
- this .logExit( "getConnection" );
- }
- return conn;
- }
- /**
- * Closes the connection and logs any exception that occurs.
- *
- * @param conn the connection to close (may be null)
- */
- private void closeConnection(Connection conn) {
- this .logEntry( "closeConnection({0})" , conn);
- try {
- if (conn != null ) {
- conn.close();
- }
- } catch (SQLException ex) {
- // this exception doesn't really affect the user, so we'll just log it and continue
- this .logException(ex);
- } finally {
- this .logExit( "closeConnection" );
- }
- }
- /**
- * Closes a prepared statement and logs any exception that occurs.
- *
- * @param statement the prepared statement to close (may be null)
- */
- private void closeStatement(PreparedStatement statement) {
- this .logEntry( "closeStatement({0})" , statement);
- try {
- if (statement != null ) {
- statement.close();
- }
- } catch (SQLException ex) {
- // this exception doesn't really affect the user, so we'll just log it and continue
- this .logException(ex);
- } finally {
- this .logExit( "closeStatement" );
- }
- }
- /**
- * Save an entity with no transaction. Transaction here is handled by the EJB container.
- */
- public void saveEntity(Entity entity) {
- this .checkObject(entity, "entity" );
- this .logEntry( "saveMessage({0})" , entity);
- Connection conn = null ;
- PreparedStatement statement = null ;
- try {
- conn = this .getConnection();
- try {
- statement = conn.prepareStatement(INSERT_DETAILS);
- int pos = 1 ; // work around checkstyle warning about "magic numbers"
- statement.setLong(pos++, entity.getMessageId());
- statement.setString(pos, entity.getText());
- statement.executeUpdate();
- } catch (SQLException ex) {
- this .throwPersistenceException(ex, "error saving message: " );
- }
- } finally {
- this .closeStatement(statement );
- this .closeConnection(conn);
- this .logExit( "saveMessage" );
- }
- }
- /**
- * Save an entity with transaction. When errors occur, we need to roll back the transaction.
- */
- public void saveEntity(Entity entity) {
- this .checkObject(entity, "entity" );
- this .logEntry( "saveMessage({0})" , entity);
- Connection conn = null ;
- PreparedStatement statement = null ;
- try {
- conn = this .getConnection();
- try {
- statement = conn.prepareStatement(INSERT_DETAILS);
- int pos = 1 ; // work around checkstyle warning about "magic numbers"
- statement.setLong(pos++, entity.getMessageId());
- statement.setString(pos, entity.getText());
- statement.executeUpdate();
- } catch (SQLException ex) {
- this .throwPersistenceException(ex, "error saving message: " );
- }
- } finally {
- this .closeStatement(statement );
- this .closeConnection(conn);
- this .logExit( "saveMessage" );
- }
- }
- /**
- * Creates a
MessagePersistenceException
based on the specified exception, logs it, and - * throws it.
- *
- * @param ex the wrapped exception
- * @param what a description of the operation that failed
- * @throws MessagePersistenceException a
MessagePersistenceException
wrapping the - * lower-level exception
- */
- private void throwPersistenceException(Throwable ex, String what) {
- // simple helper method, so no logging
- MessagePersistenceException mpe = new MessagePersistenceException(what + ex.getMessage(), ex);
- logException(mpe);
- throw mpe;
- }