package eu.etaxonomy.cdm.database;\r
\r
import java.io.File;\r
+import java.io.IOException;\r
import java.sql.Connection;\r
import java.sql.Driver;\r
import java.sql.DriverManager;\r
import java.sql.SQLException;\r
import java.util.Properties;\r
\r
+import javax.sql.DataSource;\r
+\r
+import org.apache.commons.dbcp.BasicDataSource;\r
+import org.apache.commons.pool.impl.GenericObjectPool;\r
import org.apache.log4j.Logger;\r
import org.h2.tools.Server;\r
import org.springframework.jdbc.CannotGetJdbcConnectionException;\r
-import org.springframework.jdbc.datasource.DriverManagerDataSource;\r
-//import org.springframework.jdbc.datasource.DriverManagerDataSource;\r
\r
import eu.etaxonomy.cdm.api.application.CdmApplicationUtils;\r
+import eu.etaxonomy.cdm.model.name.NomenclaturalCode;\r
\r
\r
/**\r
* @author a.mueller\r
*\r
+ * IN WORK\r
+ *\r
*/\r
\r
-public class LocalH2 extends DriverManagerDataSource {\r
+public class LocalH2 extends BasicDataSource {\r
private static final Logger logger = Logger.getLogger(LocalH2.class);\r
\r
private String sep = System.getProperty("file.separator");\r
\r
/** url without database name */\r
- protected String pureUrl = "jdbc:h2:tcp://localhost/";\r
+ protected String pureUrl = "jdbc:h2:";\r
/** database name */\r
protected String dbName = "cdm";\r
/** path, where database should be stored in the file system */\r
protected boolean isSilent = true;\r
/** default driver class name */\r
protected String DEFAULT_DRIVER_CLASS_NAME = "org.h2.Driver";\r
- \r
+ String mode = H2Mode.EMBEDDED.toString();\r
+\r
+ private NomenclaturalCode nomenclaturalCode; \r
\r
/**\r
* \r
*/\r
public LocalH2() {\r
setDriverClassName(DEFAULT_DRIVER_CLASS_NAME);\r
- setComposedUrl();\r
+ setLocalUrl();\r
}\r
\r
/**\r
* @throws CannotGetJdbcConnectionException\r
*/\r
public LocalH2(String url) throws CannotGetJdbcConnectionException {\r
- super(url);\r
+ super();\r
+ this.setUrl(url);\r
setDriverClassName(DEFAULT_DRIVER_CLASS_NAME);\r
}\r
\r
+ /* FIXME This is a workaround to solve a problem with dbcp connection pooling.\r
+ * Remove this when dbcp connection pool gets configured correctly\r
+ * \r
+ * (non-Javadoc)\r
+ * @see org.apache.commons.dbcp.BasicDataSource#createDataSource()\r
+ */\r
+ @Override\r
+ protected synchronized DataSource createDataSource() throws SQLException {\r
+ super.createDataSource();\r
+ connectionPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW);\r
+ return dataSource;\r
+ }\r
+ \r
/**\r
* @param url\r
* @param username\r
*/\r
public LocalH2(String url, String username, String password)\r
throws CannotGetJdbcConnectionException {\r
- super(url, username, password);\r
- this.setDriverClassName(DEFAULT_DRIVER_CLASS_NAME);\r
+ this(url);\r
+ this.setUsername(username);\r
+ this.setPassword(password);\r
}\r
\r
/**\r
*/\r
public LocalH2(String driverClassName, String url, String username,\r
String password) throws CannotGetJdbcConnectionException {\r
- super(driverClassName, url, username, password);\r
+ this(url, username, password);\r
+ this.setDriverClassName(driverClassName);\r
}\r
\r
+//** ********************************************************************************/\r
+ \r
public void init(){\r
+ logger.info("LocalH2init");\r
+ if (true){ //starting sever is not necessary for H2\r
+ return;\r
+ }\r
if (isStartServer){\r
this.startH2Server();\r
}\r
}\r
\r
\r
- /* (non-Javadoc)\r
- * @see org.springframework.jdbc.datasource.DriverManagerDataSource#getUrl()\r
- */\r
- @Override\r
- public String getUrl() {\r
- return super.getUrl();\r
- }\r
-\r
- /* (non-Javadoc)\r
- * @see org.springframework.jdbc.datasource.DriverManagerDataSource#setUrl(java.lang.String)\r
- */\r
- @Override\r
- public void setUrl(String url) {\r
- super.setUrl(url);\r
- }\r
-\r
- /**\r
- * @return the pureUrl\r
- */\r
- public String getPureUrl() {\r
- return pureUrl;\r
- }\r
-\r
- /**\r
- * @param pureUrl the pureUrl to set\r
- */\r
- public void setPureUrl(String pureUrl) {\r
- this.pureUrl = pureUrl;\r
- if (dbName != null){\r
- setComposedUrl();\r
- }\r
- }\r
-\r
- /**\r
- * @return the dbName\r
- */\r
- public String getDbName() {\r
- return dbName;\r
- }\r
-\r
- /**\r
- * @param dbName the dbName to set\r
- */\r
- public void setDbName(String dbName) {\r
- this.dbName = dbName;\r
- if (pureUrl != null){\r
- setComposedUrl();\r
- }\r
- }\r
- \r
- private void setComposedUrl(){\r
- setUrl(getPureUrl() + getDbName());\r
- }\r
- \r
- //checks if hsqldb-server is started, if not it will be started \r
+ //checks if h2-server is started, if not it will be started (taken over from hsqldb, maybe not necessary for H2\r
private void startH2Server(){\r
try {\r
Driver driver = DriverManager.getDriver(getUrl());\r
logger.info("Start H2Server"); \r
String[] args = new String[] { "-trace" };\r
h2Server = Server.createTcpServer(args).start();\r
- if (logger.isDebugEnabled()){\r
- for (int i = 0; i < 10; i++){\r
-// logger.info("DatabaseName " + i + ": " + h2Server.getDatabaseName(i, true));\r
-// logger.info("DatabaseName " + i + ": " + h2Server.getDatabaseName(i, false));\r
-// logger.info("DatabasePath " + i + ": " + h2Server.getDatabasePath(i, true));\r
-// logger.info("DatabasePath " + i + ": " + h2Server.getDatabasePath(i, false));\r
-// logger.info("DatabaseType " + i + ": " + h2Server.getDatabaseType(i));\r
- }\r
- }\r
// h2Server.setDatabaseName(0, getDbName());\r
// h2Server.setDatabasePath(0, getDatabasePath());\r
h2Server.start();\r
-// h2Server.checkRunning(true);\r
} catch (SQLException sqle1) {\r
- logger.error("SQL Exception when starting Local H2Server.");\r
+ logger.error("SQL Exception when starting Local H2Server: "+ sqle1);\r
} catch (RuntimeException e1) {\r
logger.error("Local H2Server could not be started or connection to existing server could not be established.");\r
}\r
}\r
\r
private static final String getDefaultPath(){\r
- //String path = System.getProperty("user.dir");\r
- File path = CdmApplicationUtils.getWritableResourceDir();\r
+ File path;\r
+ try {\r
+ path = CdmApplicationUtils.getWritableResourceDir();\r
+ } catch (IOException e) {\r
+ logger.error(e);\r
+ throw new RuntimeException(e);\r
+ }\r
String subPath = File.separator + "h2" + File.separator + "LocalH2"; \r
return path + subPath;\r
}\r
public void setStartServer(boolean isStartServer) {\r
this.isStartServer = isStartServer;\r
}\r
-\r
- /**\r
- * @return the isSilent\r
- */\r
- public boolean isSilent() {\r
- return isSilent;\r
+ \r
+ public void setLocalUrl(){\r
+ String dbName = "cdmLocal";\r
+ String localUrlString = pureUrl + "file:" + getDefaultPath() + "/" + dbName;\r
+ logger.info("setLocalUrl: " + localUrlString);\r
+ setUrl(localUrlString);\r
}\r
-\r
- /**\r
- * @param isSilent the isSilent to set\r
- */\r
- public void setSilent(boolean isSilent) {\r
- if (this.h2Server != null){\r
-// this.h2Server.setSilent(isSilent);\r
- }\r
- this.isSilent = isSilent;\r
+ \r
+ public void setMode(String mode){\r
+ this.mode = mode;\r
}\r
\r
+ public String getMode(){\r
+ return mode;\r
+ }\r
+ \r
+ public void setNomenclaturalCode(NomenclaturalCode code){\r
+ this.nomenclaturalCode = code;\r
+ }\r
\r
\r
}\r