View Javadoc

1   /***
2    * Simple Web Spider - <http://simplewebspider.sourceforge.net/>
3    * Copyright (C) 2009  <berendona@users.sourceforge.net>
4    *
5    * This program is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package simplespider.simplespider.dao.db4o;
20  
21  import java.sql.SQLException;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  
26  import simplespider.simplespider.dao.DbHelper;
27  
28  import com.db4o.ObjectContainer;
29  
30  public class Db4oDbHelper implements DbHelper {
31  
32  	private static final Log	LOG	= LogFactory.getLog(Db4oDbHelper.class);
33  
34  	private ObjectContainer		container;
35  
36  	private Db4oDbHelperFactory	helperFactory;
37  
38  	@Override
39  	public void beginTransaction() {
40  		// Nothing to do
41  
42  	}
43  
44  	@Override
45  	public void close() throws SQLException {
46  		this.container.close();
47  		this.container = null;
48  	}
49  
50  	@Override
51  	public void commitTransaction() {
52  		try {
53  			this.container.commit();
54  		} catch (final RuntimeException e) {
55  			try {
56  				this.container.rollback();
57  			} catch (final RuntimeException e2) {
58  				LOG.error("Failed to rollback database transaction", e);
59  			}
60  
61  			throw e;
62  		}
63  	}
64  
65  	@Override
66  	public Db4oLinkDao getLinkDao() {
67  		return new Db4oLinkDao(this);
68  	}
69  
70  	@Override
71  	public void rollbackTransaction() throws SQLException {
72  		this.container.rollback();
73  	}
74  
75  	@Override
76  	public void shutdown() throws SQLException {
77  		this.helperFactory.shutdown();
78  	}
79  
80  	void createConnection(final Db4oDbHelperFactory db4oDbHelperFactory) {
81  		this.helperFactory = db4oDbHelperFactory;
82  		this.container = this.helperFactory.getConnection();
83  	}
84  
85  	ObjectContainer getContainer() {
86  		return this.container;
87  	}
88  
89  }