title: Insert data into oracle with spark
body: <p>I am using spark to insert data into oracle,but I hit a problem:</p>
  
  <p>There are several tasks/partitions that write data to oracle in parellel， each task do its own part:</p>
  
  <ul>
  <li>Obtain the connection</li>
  <li>open the transaction</li>
  <li>insert the data</li>
  <li>commit the transaction</li>
  <li>rollback if task fails</li>
  </ul>
  
  <p>What if some tasks succeed,while one task fail, which means that partial data are inserted while some data are not.</p>
  
  <p>That is, I want to rollback if there is task fails.</p>
  
  <p>Following is the sample code:</p>
  
  <pre><code>object SparkJdbcOracle {
    def main(args: Array[String]) {
      val spark = SparkSession.builder().master("local[4]").appName("SparkJdbcOracle").getOrCreate()
      spark.sparkContext.parallelize(0 until 100, 4).foreachPartition(partitionIterator =&gt; {
        val conn = DriverManager.getConnection("jdbc connection")
        try {
          conn.setAutoCommit(false)
          val stmt = conn.createStatement()
          partitionIterator.foreach(i =&gt; {
            stmt.addBatch(s"insert into A(id) values ($i)")
          })
          stmt.executeBatch()
          conn.commit()
        } catch {
          case e: Exception =&gt; {
            conn.rollback()
            throw e
          }
        }
  
  
      })
    }
  
  }
  </code></pre>