01: /*
02: * Copyright 2004-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.apache.lucene.store.jdbc;
18:
19: import java.sql.Connection;
20:
21: import org.apache.lucene.index.IndexWriter;
22: import org.apache.lucene.store.Lock;
23: import org.apache.lucene.store.jdbc.datasource.DataSourceUtils;
24: import org.apache.lucene.store.jdbc.support.JdbcTable;
25:
26: /**
27: * @author kimchy
28: */
29: public class JdbcDirectoryLockTests extends AbstractJdbcDirectoryTests {
30:
31: private boolean DISABLE = true;
32:
33: public void testLocks() throws Exception {
34: if (DISABLE) {
35: return;
36: }
37: JdbcDirectorySettings settings = new JdbcDirectorySettings();
38: settings.setQueryTimeout(1);
39:
40: JdbcDirectory dir1 = new JdbcDirectory(dataSource,
41: new JdbcTable(settings, createDialect(), "TEST"));
42: Connection con1 = DataSourceUtils.getConnection(dataSource);
43: dir1.create();
44: DataSourceUtils.commitConnectionIfPossible(con1);
45: DataSourceUtils.releaseConnection(con1);
46:
47: JdbcDirectory dir2 = new JdbcDirectory(dataSource,
48: new JdbcTable(settings, createDialect(), "TEST"));
49:
50: try {
51: // shoudl work
52: con1 = DataSourceUtils.getConnection(dataSource);
53: Lock lock1 = dir1.makeLock(IndexWriter.WRITE_LOCK_NAME);
54:
55: boolean obtained = lock1.obtain();
56: assertTrue(obtained);
57:
58: Connection con2 = DataSourceUtils.getConnection(dataSource);
59: Lock lock2 = dir2.makeLock(IndexWriter.WRITE_LOCK_NAME);
60:
61: obtained = lock2.obtain();
62: assertFalse(obtained);
63:
64: obtained = lock2.obtain();
65: assertFalse(obtained);
66:
67: lock1.release();
68:
69: DataSourceUtils.commitConnectionIfPossible(con1);
70: DataSourceUtils.releaseConnection(con1);
71:
72: obtained = lock2.obtain();
73: assertTrue(obtained);
74:
75: DataSourceUtils.commitConnectionIfPossible(con2);
76: DataSourceUtils.releaseConnection(con2);
77: } finally {
78: try {
79: con1 = DataSourceUtils.getConnection(dataSource);
80: dir1.delete();
81: DataSourceUtils.commitConnectionIfPossible(con1);
82: DataSourceUtils.releaseConnection(con1);
83: } catch (Exception e) {
84: e.printStackTrace(System.out);
85: }
86: }
87: }
88: }
|