List of usage examples for org.apache.commons.pool.impl GenericObjectPool GenericObjectPool
public GenericObjectPool(PoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle)
From source file:com.zimbra.cs.db.DbPool.java
/** Initializes the connection pool. */ private static synchronized PoolingDataSource getPool() { if (isShutdown) throw new RuntimeException("DbPool permanently shutdown"); if (sPoolingDataSource != null) return sPoolingDataSource; PoolConfig pconfig = Db.getInstance().getPoolConfig(); sConnectionPool = new GenericObjectPool(null, pconfig.mPoolSize, pconfig.whenExhaustedAction, -1, pconfig.mPoolSize);//from w w w . ja v a 2s . c om ConnectionFactory cfac = ZimbraConnectionFactory.getConnectionFactory(pconfig); boolean defAutoCommit = false, defReadOnly = false; new PoolableConnectionFactory(cfac, sConnectionPool, null, null, defReadOnly, defAutoCommit); try { Class.forName(pconfig.mDriverClassName).newInstance(); //derby requires the .newInstance() call Class.forName("org.apache.commons.dbcp.PoolingDriver"); } catch (Exception e) { ZimbraLog.system.fatal("can't instantiate DB driver/pool class", e); System.exit(1); } try { PoolingDataSource pds = new PoolingDataSource(sConnectionPool); pds.setAccessToUnderlyingConnectionAllowed(true); Db.getInstance().startup(pds, pconfig.mPoolSize); sPoolingDataSource = pds; } catch (SQLException e) { ZimbraLog.system.fatal("can't initialize connection pool", e); System.exit(1); } if (pconfig.mSupportsStatsCallback) ZimbraPerf.addStatsCallback(new DbStats()); return sPoolingDataSource; }
From source file:org.apache.wookie.beans.jcr.JCRPersistenceManager.java
/** * Initialize implementation with configuration. * /*from w w w. j a v a 2s . c o m*/ * @param configuration configuration properties * @param initializeStore truncate and initialize persistent store */ public static void initialize(Configuration configuration, boolean initializeStore) { try { // configuration repositoryUser = configuration.getString(PERSISTENCE_MANAGER_USER_PROPERTY_NAME); repositoryPassword = configuration.getString(PERSISTENCE_MANAGER_PASSWORD_PROPERTY_NAME); repositoryWorkspace = configuration.getString(PERSISTENCE_MANAGER_WORKSPACE_PROPERTY_NAME); rootPath = configuration.getString(PERSISTENCE_MANAGER_ROOT_PATH_PROPERTY_NAME); for (Map.Entry<Class<? extends IBean>, Class<? extends IBean>> mapping : BEAN_INTERFACE_TO_CLASS_MAP .entrySet()) { Class<? extends IBean> beanClass = mapping.getValue(); Class<? extends IBean> beanInterface = mapping.getKey(); String name = beanInterface.getSimpleName(); if (name.startsWith("I")) { name = name.substring(1); } if (!name.endsWith("s")) { name = name + "s"; } String nodeRootPath = rootPath + "/" + name; beanClassNodeRootPaths.put(beanClass, nodeRootPath); } // create JCR credentials session pool PoolableObjectFactory sessionFactory = new BasePoolableObjectFactory() { /* (non-Javadoc) * @see org.apache.commons.pool.BasePoolableObjectFactory#passivateObject(java.lang.Object) */ public void passivateObject(Object obj) throws Exception { // clear OCM object cache ((ObjectContentManagerImpl) obj).setRequestObjectCache(new RequestObjectCacheImpl()); } /* (non-Javadoc) * @see org.apache.commons.pool.BasePoolableObjectFactory#makeObject() */ public Object makeObject() throws Exception { // lookup JCR repository from context Context initialContext = new InitialContext(); Repository repository = (Repository) initialContext .lookup(WIDGET_REPOSITORY_JNDI_REPOSITORY_FULL_NAME); // create and login JCR session Credentials credentials = new SimpleCredentials(repositoryUser, repositoryPassword.toCharArray()); Session session = ((repositoryWorkspace != null) ? repository.login(credentials, repositoryWorkspace) : repository.login(credentials)); // return session object content manager for session return new SessionObjectContentManagerImpl(session, new AnnotationMapperImpl(CLASS_LIST)); } /* (non-Javadoc) * @see org.apache.commons.pool.BasePoolableObjectFactory#destroyObject(java.lang.Object) */ public void destroyObject(Object obj) throws Exception { // logout and close object content manager and session ((ObjectContentManagerImpl) obj).logout(); } }; ocmPool = new GenericObjectPool(sessionFactory, 0, GenericObjectPool.WHEN_EXHAUSTED_GROW, 0, 5); ocmPool.setTimeBetweenEvictionRunsMillis(60000); ocmPool.setMinEvictableIdleTimeMillis(300000); // initialize persistent store if (initializeStore) { // borrow object content manager and initialization session from pool ObjectContentManager ocm = (ObjectContentManager) ocmPool.borrowObject(); Session session = ocm.getSession(); // initialize root path in repository // Jackrabbit/JCR 2.X //boolean rootPathNodeExists = session.nodeExists(rootPath); // Jackrabbit/JCR 1.X boolean rootPathNodeExists = session.itemExists(rootPath); if (rootPathNodeExists) { // delete nodes of root path node // Jackrabbit/JCR 2.X //Node rootNode = session.getNode(rootPath); // Jackrabbit/JCR 1.X Node rootNode = (Node) session.getItem(rootPath); NodeIterator nodesIter = rootNode.getNodes(); while (nodesIter.hasNext()) { nodesIter.nextNode().remove(); } } else { // create unstructured node hierarchy int rootPathParentIndex = -1; int rootPathIndex = rootPath.indexOf('/', 1); while (rootPathIndex != -1) { // Jackrabbit/JCR 2.X //Node parentNode = session.getNode(rootPath.substring(0, ((rootPathParentIndex != -1) ? rootPathParentIndex : 1))); // Jackrabbit/JCR 1.X Node parentNode = (Node) session.getItem( rootPath.substring(0, ((rootPathParentIndex != -1) ? rootPathParentIndex : 1))); String nodeName = rootPath.substring( ((rootPathParentIndex != -1) ? rootPathParentIndex + 1 : 1), rootPathIndex); parentNode.addNode(nodeName, "nt:unstructured"); rootPathParentIndex = rootPathIndex; rootPathIndex = rootPath.indexOf('/', rootPathIndex + 1); } // Jackrabbit/JCR 2.X //Node parentNode = session.getNode(rootPath.substring(0, ((rootPathParentIndex != -1) ? rootPathParentIndex : 1))); // Jackrabbit/JCR 1.X Node parentNode = (Node) session.getItem( rootPath.substring(0, ((rootPathParentIndex != -1) ? rootPathParentIndex : 1))); String nodeName = rootPath .substring(((rootPathParentIndex != -1) ? rootPathParentIndex + 1 : 1)); parentNode.addNode(nodeName, "nt:unstructured"); } // create bean class node root paths // Jackrabbit/JCR 2.X //Node rootNode = session.getNode(rootPath); // Jackrabbit/JCR 1.X Node rootNode = (Node) session.getItem(rootPath); for (String nodeRootPath : beanClassNodeRootPaths.values()) { String nodeName = nodeRootPath.substring(rootPath.length() + 1); rootNode.addNode(nodeName, "nt:unstructured"); } session.save(); // register/reregister repository node types NodeTypeManager nodeTypeManager = session.getWorkspace().getNodeTypeManager(); InputStream nodeTypesCNDStream = JCRPersistenceManager.class .getResourceAsStream("wookie-schema.cnd"); if (nodeTypesCNDStream == null) { throw new IllegalArgumentException( "Unable to load node types configuration: wookie-schema.cnd"); } // Jackrabbit/JCR 2.X //Reader nodeTypesCNDReader = new InputStreamReader(nodeTypesCNDStream); //NamespaceRegistry namespaceRegistry = session.getWorkspace().getNamespaceRegistry(); //ValueFactory valueFactory = session.getValueFactory(); //CndImporter.registerNodeTypes(nodeTypesCNDReader, "wookie-schema.cnd", nodeTypeManager, namespaceRegistry, valueFactory, true); // Jackrabbit/JCR 1.X ((NodeTypeManagerImpl) nodeTypeManager).registerNodeTypes(nodeTypesCNDStream, NodeTypeManagerImpl.TEXT_X_JCR_CND, true); // save session used to load node types session.save(); logger.info("Persistent store initialized at " + rootPath); // return object content manager and initialization session to pool ocmPool.returnObject(ocm); } logger.info("Initialized"); } catch (Exception e) { throw new RuntimeException("Unable to initialize: " + e, e); } }
From source file:org.apache.wookie.tests.beans.JPAPersistenceTest.java
/** * Set up JPA persistence runtime test environment. * // www.ja v a 2s . co m * @throws Exception */ @Before public void setUpPerTest() throws Exception { logger.info("JPA set up test"); // load database configuration from environment String testDatabaseDir = (System.getProperty("user.dir") + "/build/test-database") .replace(File.separatorChar, '/'); dbUser = getSystemProperty(DB_USER_PROPERTY_NAME, "java"); dbPassword = getSystemProperty(DB_PASSWORD_PROPERTY_NAME, "java"); dbDriver = getSystemProperty(DB_DRIVER_CLASS_PROPERTY_NAME, "org.apache.derby.jdbc.EmbeddedDriver"); dbUri = getSystemProperty(DB_URI_PROPERTY_NAME, "jdbc:derby:" + testDatabaseDir + "/widgetdb;create=true"); dbType = getSystemProperty(DB_TYPE_PROPERTY_NAME, "derby"); // load driver Class.forName(dbDriver); // test connection Connection conn = DriverManager.getConnection(dbUri, dbUser, dbPassword); conn.close(); // construct pooling datasource connectionPool = new GenericObjectPool(null, 0, GenericObjectPool.WHEN_EXHAUSTED_GROW, 0, 5); ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(dbUri, dbUser, dbPassword); new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true); DataSource dataSource = new PoolingDataSource(connectionPool); // create JNDI context rootContext = new InitialContext(); Context namingContext = lookupOrCreateNamingContext(rootContext, "java:comp"); namingContext = lookupOrCreateNamingContext(namingContext, "env"); lookupOrCreateNamingContext(namingContext, "jdbc"); // bind datasource to JNDI context rootContext.rebind(JPAPersistenceManager.WIDGET_DATABASE_JNDI_DATASOURCE_FULL_NAME, dataSource); // initialize persistence manager factory and persistence manager; // truncate and initialize persistent store Configuration configuration = new PropertiesConfiguration(); configuration.setProperty(PersistenceManagerFactory.PERSISTENCE_MANAGER_CLASS_NAME_PROPERTY_NAME, JPAPersistenceManager.class.getName()); configuration.setProperty(PersistenceManagerFactory.PERSISTENCE_MANAGER_INITIALIZE_STORE_PROPERTY_NAME, "true"); configuration.setProperty(JPAPersistenceManager.PERSISTENCE_MANAGER_CACHE_SIZE_PROPERTY_NAME, "1000"); configuration.setProperty(JPAPersistenceManager.PERSISTENCE_MANAGER_DB_TYPE_PROPERTY_NAME, dbType); PersistenceManagerFactory.initialize(configuration); logger.info("JPA test set up"); configured = true; }