`

java数据库访问标准格式(no transaction)

阅读更多
java 代码
 
  1.      /**  
  2.      * The SQL statement to insert the entity details.  
  3.      */   
  4.     private   static   final  String INSERT_DETAILS =  "INSERT INTO message (message_id, from_handle, project_id, "   
  5.                     + "project_name, timestamp, subject, message) VALUES (?, ?, ?, ?, ?, ?, ?)" ;  
  6.    
  7.    /**  
  8.      * Creates a new database connection using the configured database connection factory and connection name.  
  9.      *  
  10.      * @return a new database connection created using the configured database connection factory and  
  11.      *         connection name  
  12.      * @throws MessagePersistenceException if an error occurs while creating the connection  
  13.      */   
  14.     private  Connection getConnection() {  
  15.         logEntry("getConnection()" );  
  16.   
  17.         Connection conn = null ;  
  18.   
  19.         try  {  
  20.             this .logExternalCall( "calling connectionFactory.createConnection({0})" , connectionName);  
  21.             conn = this .connectionFactory.createConnection(connectionName);  
  22.             this .logExternalCall( "returned from connectionFactory.createConnection({0})" , connectionName);  
  23.         } catch  (DBConnectionException ex) {  
  24.             this .throwPersistenceException(ex,  "error creating connection: " );  
  25.         } finally  {  
  26.             this .logExit( "getConnection" );  
  27.         }  
  28.   
  29.         return  conn;  
  30.     }  
  31.   
  32.     /**  
  33.      * Closes the connection and logs any exception that occurs.  
  34.      *  
  35.      * @param conn the connection to close (may be null)  
  36.      */   
  37.     private   void  closeConnection(Connection conn) {  
  38.         this .logEntry( "closeConnection({0})" , conn);  
  39.   
  40.         try  {  
  41.             if  (conn !=  null ) {  
  42.                 conn.close();  
  43.             }  
  44.         } catch  (SQLException ex) {  
  45.             // this exception doesn't really affect the user, so we'll just log it and continue   
  46.             this .logException(ex);  
  47.         } finally  {  
  48.             this .logExit( "closeConnection" );  
  49.         }  
  50.     }  
  51.   
  52.   
  53.     /**  
  54.      * Closes a prepared statement and logs any exception that occurs.  
  55.      *  
  56.      * @param statement the prepared statement to close (may be null)  
  57.      */   
  58.     private   void  closeStatement(PreparedStatement statement) {  
  59.         this .logEntry( "closeStatement({0})" , statement);  
  60.   
  61.         try  {  
  62.             if  (statement !=  null ) {  
  63.                 statement.close();  
  64.             }  
  65.         } catch  (SQLException ex) {  
  66.             // this exception doesn't really affect the user, so we'll just log it and continue   
  67.             this .logException(ex);  
  68.         } finally  {  
  69.             this .logExit( "closeStatement" );  
  70.         }  
  71.     }  
  72.   
  73. /**  
  74.  *  Save an entity with no transaction. Transaction here is handled by the EJB container.  
  75.  */   
  76.     public   void  saveEntity(Entity entity) {  
  77.         this .checkObject(entity,  "entity" );  
  78.         this .logEntry( "saveMessage({0})" , entity);  
  79.   
  80.         Connection conn = null ;  
  81.         PreparedStatement statement = null ;  
  82.   
  83.         try  {  
  84.             conn = this .getConnection();  
  85.   
  86.             try  {  
  87.                 statement = conn.prepareStatement(INSERT_DETAILS);  
  88.   
  89.                 int  pos =  1 // work around checkstyle warning about "magic numbers"   
  90.                 statement.setLong(pos++, entity.getMessageId());  
  91.                 statement.setString(pos, entity.getText());  
  92.   
  93.                 statement.executeUpdate();  
  94.             } catch  (SQLException ex) {  
  95.                 this .throwPersistenceException(ex,  "error saving message: " );  
  96.             }  
  97.         } finally  {  
  98.             this .closeStatement(statement );  
  99.             this .closeConnection(conn);  
  100.             this .logExit( "saveMessage" );  
  101.         }  
  102.     }  
  103.   
  104. /**  
  105.  *  Save an entity with transaction.  When errors occur, we need to roll back the transaction.  
  106.  */   
  107.     public   void  saveEntity(Entity entity) {  
  108.         this .checkObject(entity,  "entity" );  
  109.         this .logEntry( "saveMessage({0})" , entity);  
  110.   
  111.         Connection conn = null ;  
  112.         PreparedStatement statement = null ;  
  113.   
  114.         try  {  
  115.             conn = this .getConnection();  
  116.   
  117.             try  {  
  118.                 statement = conn.prepareStatement(INSERT_DETAILS);  
  119.   
  120.                 int  pos =  1 // work around checkstyle warning about "magic numbers"   
  121.                 statement.setLong(pos++, entity.getMessageId());  
  122.                 statement.setString(pos, entity.getText());  
  123.   
  124.                 statement.executeUpdate();  
  125.             } catch  (SQLException ex) {  
  126.                 this .throwPersistenceException(ex,  "error saving message: " );  
  127.             }  
  128.         } finally  {  
  129.             this .closeStatement(statement );  
  130.             this .closeConnection(conn);  
  131.             this .logExit( "saveMessage" );  
  132.         }  
  133.     }  
  134.     /**  
  135.      * Creates a MessagePersistenceException  based on the specified exception, logs it, and  
  136.      * throws it.  
  137.      *  
  138.      * @param ex the wrapped exception  
  139.      * @param what a description of the operation that failed  
  140.      * @throws MessagePersistenceException a MessagePersistenceException  wrapping the  
  141.      *             lower-level exception  
  142.      */   
  143.     private   void  throwPersistenceException(Throwable ex, String what) {  
  144.         // simple helper method, so no logging   
  145.   
  146.         MessagePersistenceException mpe = new  MessagePersistenceException(what + ex.getMessage(), ex);  
  147.         logException(mpe);  
  148.         throw  mpe;  
  149.     }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics